DEV Community

Cover image for Python Built-in String Methods/ Functions 1
chryzcode
chryzcode

Posted on

Python Built-in String Methods/ Functions 1

These are basic string functions() in python🐍.

So, let the journey🏁 begin🚀....

- find()

The find() method finds the first occurrence of the specified value.
The find() method returns -1 if the value is not found.

input : string = 'python dev'
f = input.find(dev)
print (f)
Output : 7
w = input.find(z)
print(w)
Output : -1
Enter fullscreen mode Exit fullscreen mode

- format()

The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}

input : string = "For only {price:.2f} dollars!"
print(input.format(price = 49))
Output: 
For only 49.00 dollars!
Enter fullscreen mode Exit fullscreen mode

- index()

This method returns the position at the first occurrence of the specified value.

fruits : string = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
Output: 2
Enter fullscreen mode Exit fullscreen mode

- isalnum()

isalnum() function in Python programming language checks whether all the characters in a given string is alphanumeric or not.
Alphanumeric:A character that is either a letter or a number

Input : string = 'abc123'
Output : True
Input : string = 'abc 123'
Output : False
Enter fullscreen mode Exit fullscreen mode

- isalpha()

The isalpha() methods returns true if all characters in the string are alphabets otherwise, It returns false.

Input : string = 'Developer'
Output : True
Input : string = 'The boy'
Output : False
Input : string = 'Devs0212'
Output : False
Enter fullscreen mode Exit fullscreen mode

- isdecimal()

It is a function in Python that returns true if all characters in a string are decimal. If all characters are not decimal then it returns false.

input : string = '12345'
Output : True
input : string = '12e3y45'
Output : False
input : string = '12 45'
Output : False
Enter fullscreen mode Exit fullscreen mode

- isdigit()

isdigit() is a built-in method used for string handling.
The isdigit() method returns true if all characters in the string are digits otherwise, It returns false

Input : string = '15460'
Output : True
Input : string = '154ban60'
Output : False
Enter fullscreen mode Exit fullscreen mode

- isidentifier()

The isidentifier() method returns true if the string is a valid identifier, otherwise false.
A string is considered a valid identifier if it only contains alphanumeric letters (a-z) and (0-9), or underscores (_). A valid identifier cannot start with a number, or contain any spaces.

input: 'Mydemo'
print(input.isidentifier())
Output: True
input: 'My demo'
print(input.isidentifier())
Output: False
Enter fullscreen mode Exit fullscreen mode

- isnumeric()

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.
Exponents, like ² and ¾ are also considered to be numeric values. "-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the . are not.

input: '1235'
Output : True
input: '12b3o5'
Output : False 
Enter fullscreen mode Exit fullscreen mode

- isspace( )

The isspace() methods returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”.

Input : string = '\n \n \n'
Output : True
Input : string = 't\n h\n e\n'
Output : False
Enter fullscreen mode Exit fullscreen mode

- istitle( )

The istitle() method returns true if all words in a text start with a upper case letter, and the rest of the word are lower case letters, otherwise false. The function "istitlte()" does not take any parameter!

input : string = 'Hello'
print(text.istitle())
Output: True
input : string = 'hello'
print(text.istitle())
Output: False
Enter fullscreen mode Exit fullscreen mode

- isprintable( )

The isprintable() method returns true if all the characters are printable, otherwise false.

input : string = 
'Hello'
Output: True
input : string = 
'Hello!\nAre you #1?'
Output: False
Enter fullscreen mode Exit fullscreen mode

- isupper()

The isupper() methods returns true if all characters in the string are uppercase otherwise, It returns false. The function "isupper()" does not take any parameter!

Input : string 'HASHNODE'
Output : True
Input : string  'hashnode'
Output : False
Enter fullscreen mode Exit fullscreen mode

- islower( )

The islower() methods returns true if all characters in the string are lowercase otherwise, It returns false
This function is used to check if the argument contains any lowercase characters

Input : string = 'python'
Output : True
Input : string = 'Python' Output : False
Enter fullscreen mode Exit fullscreen mode

- upper( )

The upper() methods returns the uppercased string from the given string. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string.

Input : string = 'coding'
Output : CODING
Input : string = 'CODING'
Output : CODING
Enter fullscreen mode Exit fullscreen mode

- lower()

The lower() methods returns the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string.

Input : string = 'PYTHONISTA'
Output : pythonista
Input : string = 'PythoNista'
Output : pythonista
Enter fullscreen mode Exit fullscreen mode

Please, kindly like and comment below for constructive criticism and feedback.
You can also link up with me on this platform

Top comments (0)