Strings in Python are objects of the str data type, and they have a wide range of methods that allow you to manipulate and work with text data. Python has a couple of in-built methods which can be used to handle operations on string. Each method on a string does not alter the original string; instead, it returns a new string with the modified characteristics.
Python String Methods
Some commonly used methods of string in Python:
isalpha()
isdigit()
split()
replace()
upper()
Strings are highly versatile, and these methods allow you to perform a wide range of operations on them to manipulate and extract information from text data.
Method
Description
Example
capitalize()
returns a string copy with the first letter in uppercase
>>> text="hello"
>>> text.capitalize()
'Hello'
casefold()
returns a string copy with the all letter in lowercase
>>> text="HELLO"
>>> text.casefold()
'hello'
center()
returns a centered string copy
>>> text="HELLO"
>>> text.center(10)
' HELLO '
count()
returns number of occurences of a specific value in the given string
>>> text="HELLO"
>>> text.count('L')
2
encode()
returns encoded version of the string
>>> text="HELLO"
>>> text.encode()
b'HELLO'
endswith()
returns True if the given string ends with given value else returns False
>>> text="HELLO"
>>> text.endswith('O')
True
find()
returns 0 if the given sub-string is present else returns -1
>>> text="HELLO"
>>> text.find('E')
0
index()
returns the index of the element in the string, this method throws exception if the element is not present in the string.
>>> text="HELLO"
>>> text.index('E')
1
isalnum()
returns True if the given string contains only alpha-numeric values
>>> text="HELLO"
>>> text.isalnum()
True
isalpha()
returns True if string contains only alphabets
>>> text="HELLO"
>>> text.isalpha()
True
isdigit()
returns True if the string contains only digits else returns False
>>> text="HELLO"
>>> text.isdigit()
False
isidentifier()
returns True if the string only contains alphanumeric letters (a-z) and (0-9), or underscores (_). A valid identifier cannot start with a number, or contain any spaces.
>>> text="HELLO"
>>> text.isidentifier()
True
islower()
returns True if the string contains all the characters are in lowercase
>>> text="hello1"
>>> text.islower()
True
isnumeric()
returns True if all the characters in the string are numrical else returns False
>>> text="HELLO"
>>> text.isnumeric()
False
isspace()
returns True if the string contains whitespaces only
>>> text="hello1"
>>> text.isspace()
False
isupper()
returns True if the string contains all the characters are in uppercase
>>> text="HELLO1"
>>> text.isupper()
True
lower()
returns a copy of string with all characters in lowercase
>>> text="HELLO"
>>> text.lower()
'hello'
partition()
returns a tuple with 3 strings partitioned by the argument
returns a copy of string with all characters in uppercase
>>> text="hello"
>>> text.upper()
'HELLO'
Note : As Strings are immutable data types, all the string methods returns a copy of string instead of changing the original string as string data types cannot be changed after their creation.
Login/Signup to comment