DEV Community

AJITH D R
AJITH D R

Posted on

Python String Methods

1.format():
The format() method is used to insert values into a string. It takes a string as a template and replaces any placeholders with the corresponding values.
Example:

str = "I am {} {} method."
print(str.format("learning", "format()"))
#Output: I am learning format() method.
Enter fullscreen mode Exit fullscreen mode

2.capitalize():
The capitalize() method is used to convert the first character of a string to uppercase, and all other characters to lowercase.
Example:

string = 'python Language'
print(string.capitalize()) #Output: 'Python language'
Enter fullscreen mode Exit fullscreen mode

The above line does not change the original string. To modify the original string, we should assign it to the original string variable.


3.upper():
The upper() method is used to convert all the characters in a string to uppercase.

string = 'python'
string = string.upper()
print(string) #Output: 'PYTHON'
Enter fullscreen mode Exit fullscreen mode

We can use isupper() method on a string to check if all the characters in a string are in upper-case.


4.lower():
The lower() method is used to convert all the characters in a string to lowercase.

string = 'Python'
print(string.lower()) #Output: 'python'
Enter fullscreen mode Exit fullscreen mode

We can use islower() method on a string to check if all the characters in a string are in lower-case.


5.strip():
The strip() method removes any leading and trailing spaces from a string.

string = '    python    '
print(string.strip()) #Output: 'python'
Enter fullscreen mode Exit fullscreen mode

There are also lstrip() and rstrip() methods to strip only the left-end spaces and right-end spaces.


6.join():
The join() method is used to join all the string values inside a list with a specified separator.

list_of_strings = ['abc','def','ghi']
print(''.join(list_of_strings)) #Output: abcdefghi

print(', '.join(list_of_strings)) #Output: abc, def, ghi
Enter fullscreen mode Exit fullscreen mode

7.isalnum():
The isalnum() method takes a string as an argument and returns True if all the characters in that string are alpanumeric, otherwise returns False.

string = 'Python3'
print(string.isalnum()) #True

string = 'Python&3'
print(string.isalnum()) #False
Enter fullscreen mode Exit fullscreen mode

8.isalpha():
The isalpha() method takes a string as an argument and returns True if all the characters in that string are alpabets, otherwise returns False.

string = 'Python'
print(string.isalpha()) #True

string = 'Python3'
print(string.isalpha()) #False
Enter fullscreen mode Exit fullscreen mode

9.isdigit():
The isdigit() method takes a string as an argument and returns True if all the characters in that string are digits, otherwise returns False.

string = '123'
print(string.isdigit()) #True

string = 'python'
print(string.isdigit()) #False
Enter fullscreen mode Exit fullscreen mode

10.split():
The split() method is used to convert a string to a list of strings by splitting up the string at the specified separator.

languages = 'Python,Java,JavaScript'
print(languages.split(',')) # ['Python', 'Java', 'JavaScript']
Enter fullscreen mode Exit fullscreen mode

11.startswith():
The startswith() method is used to check whether a string starts with specified prefix or not. It returns True if the string starts with the specified string.

string = 'Python language'
print(string.startswith('Pyth')) # True

string = 'Python language'
print(string.startswith('pyth    ')) # False
Enter fullscreen mode Exit fullscreen mode

12.endswith():
The endswith() method is used to check whether a string ends with specified suffix or not. It returns True if the string ends with the specified string.

string = 'Python language'
print(string.endswith('uage')) # True
Enter fullscreen mode Exit fullscreen mode

13.replace():
The replace() method is used to replace every occurrence of matching substring with another string.

line = 'Python is a snake'
line = line.replace('snake', 'language') #Python is a language
Enter fullscreen mode Exit fullscreen mode

14.index():
The index() method returns the index of first occurrence of the given substring. If the given substring is not in the string, then it raises an exception.
We can also give start and end index in which we want to find the substring.

line = 'Hello world'
print(line.index('world')) #6

print(line.index('hello')) #ValueError: substring not found

print(line.index('o', 6, 10)) #7
Enter fullscreen mode Exit fullscreen mode

15.find():
The find() method is same as index() method. But, it returns -1 when substring is not found.

line = 'Hello world'
print(line.index('world')) #6

print(line.index('hello')) #-1
Enter fullscreen mode Exit fullscreen mode

References:

Top comments (0)