Python String Methods

Python string methods

Python String Methods

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
>>> text="HELLO"
>>> text.partition('L')
('HE','L','LO')
replace() returns a copy of string with a specific value replaced by another given value
>>> text="HELLO"
>>> text.replace("L","l")
'HEllO'
split() returns a list of strings splited by whitespaces by default if no character is passed as arguments
>>> text="Hello Prepsters! Welcome to Python Tutorials "
>>> text.split()
['Hello','Prepsters!','Welcome,'to','the','Python','Tutorials']
upper() 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.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription