Method | Description |
---|---|
capitalize() | Converts the first character of the string to the upper case and remaining string to the lower case. |
center() | Returns the centered string adding fill character to right and left. |
count() | Count of number of occurrences of a substring. |
endswith() | Checks if a string ends with specified character(s). |
expandtabs() | Expands the tabs(\t) with a specified number of spaces. |
find() | Finds specific character(s) from a string. |
index() | Index location of a substring from the string. |
isdigit() | Checks if the variable contains only digits. |
islower() | To check if the specified string is in lower case or not. |
isnumeric() | If the string is numeric or not. |
isspace() | Checks if the string is just a white space(s). |
istitle() | Checks if the string is a title or not. |
isupper() | Checks if the specified string is in upper case or not. |
join() | Joins each character of the string with the new string. |
length() | Finds the length of characters in the string. |
lower() | To convert all characters of the string to the lower case. |
lstrip() | To remove the left white spaces from the string. |
replace() | To replace character(s) from the string with a new character(s). |
rstrip() | To remove the right white spaces from the string. |
split() | Split the string based on the separator. |
startswith() | To check if string starts with specified character(s). |
strip() | To remove white spaces from both sides of the string. |
swapcase() | Swaps the lower case character to upper and upper case to lower. |
title() | The first character of each word is converted to upper case. |
upper() | To convert all characters of the string to the upper case. |
Python String capitalize()
Method
The Python string capitalize()
method returns a string by converting the first character to the upper case and the remaining string to the lower case.
Note: Observe in the example below, the only first character is converted to upper case and if there is any other upper case character, it is converted into lower case.
text = 'how arE you? What are you doing today?'
text.capitalize()
Python String center()
Method
It will create the string with a specified width and will fill the gap with the fill character. The original string will be placed at the center.
The default fill character is space, but we can use any custom fill character.
Default space as fillchar
text = 'Tim'
text.center(10)
‘a’ as fillchar
text = 'Tim'
text.center(10, 'a')
Python String count()
Method
Returns the count of occurrences of a substring from the string. It has an optional start
and end
index to search within that specified range. The default value of the start index is 0 and the end index is the end of the string.
Note: Here, the end index means up to the index position. The character in that position is not included.
text = 'How are you? What are you doing?'
text.count('are')
With start and end index. Now the number of occurrences is only 1.
text = 'How are you? What are you doing?'
text.count('are',0,7)
When character(s) is not available in the string.
text = 'How are you? What are you doing?'
text.count('to')
Python String endswith()
Method
To check if a string ends with the specified character(s). Returns ‘True’ if the string ends with the character(s), else ‘False’. It has an optional start
and end
index to search within that specified range. The default value of the start index is 0 and the end index is the end of the string.
Note: Here, the end index means up to the index position. The character in that position is not included.
name = 'Will Smith'
name.endswith('g')
name = 'Will Smith'
name.endswith('h')
With Start and End index.
name = 'Will Smith'
name.endswith('h',0,6)
name = 'Will Smith'
name.endswith('S',0,6)
Python String expandtabs()
Method
It expands the tabs with a specified number of spaces. The number of spaces (tab size) parameter is an optional argument, with a default value of 8.
Print without expandtabs.
text = 'Hey,\thow are you?'
print(text)
expandtabs() with default tabsize.
text = 'Hey,\thow are you?'
print(text.expandtabs())
tabsize 16.
text = 'Hey,\thow are you?'
print(text.expandtabs(16))
Python String find()
Method
To find a specific substring from a string. It will return the index position if a substring is found, else returns -1. It has an optional start
and end
index to search within that specified range. The default value of the start index is 0 and the end index is the end of the string.
Note: Here, the end index means up to the index position. The character in that position is not included.
text = 'how are you?'
text.find('a')
text.find('are')
When character is not there in the string, then it will return -1.
text.find('q')
With start and end index.
text.find('are',0,3)
text.find('are',4,7)
Python String index()
Method
Returns the index position of the substring from the string. If the substring is not found, it returns an error. It has an optional start
and end
index to search within that specified range. The default value of the start index is 0 and the end index is the end of the string.
Note: Here, the end
index means up to the index position. The character at the end position is not included.
text = 'how are you?'
text.index('are')
When substring not found.
text.index('not')
With start and end index.
text.index('are',3,7)
With start and end index, substring not found.
text.index('are',0,4)
Python String isdigit()
Method
To check if the variable contains only digits. Returns True if the variable contains only digits, else False.
name = 'Will Smith'
name.isdigit()
number = '450'
number.isdigit()
Python String islower()
Method
To check whether the specified string is in lowercase or not. Returns True if it is lowercase, else False.
name = 'WILL SMITH'
name.islower()
name = 'will smith'
name.islower()
Python String isnumeric()
Method
If the string is numeric or not. Returns True if it is numeric, else False.
number = '125'
number.isnumeric()
number = '125d'
number.isnumeric()
Python String isspace()
Method
Checks if the string is just a whitespace(s). Returns True if the string is only whitespace(s), else False.
name = ' '
name.isspace()
name = ' will smith'
name.isspace()
Python String istitle()
Method
Checks if the string is a title or not. Returns True if it is a title, else False.
name = 'The Hero'
name.istitle()
name = 'Will smith'
name.istitle()
Python String isupper()
Method
Checks if the specified string is in upper case or not. Returns True if it is upper case, else False.
name = 'WILL SMITH'
name.isupper()
name = 'will smith'
name.isupper()
Python String join()
Method
Joins each character of the string with the new string.
text = 'how are you?'
("ABC").join(text)
Python String length()
Method
Finds the length of characters in the string, including whitespace.
text = 'Hello! Peter Cruise'
len(text)
Python String lower()
Method
To convert all characters of the string to lowercase.
name = 'Peter Cruise'
name.lower()
Python String lstrip()
Method
To remove the left whitespace(s) from the string. By default, it will remove whitespace(s), but we can pass a left substring that we want to trim.
By default it will trim whitespaces.
text = ' how are you? '
text.lstrip()
Trimming a substring ‘how’
text = 'how are you? '
text.lstrip('how')
It compares each character of the substring with the string and if it is found then remove it.
text = 'howh are you? '
text.lstrip('Hhellowa ')
Python String replace()
Method
To replace a character(s) from the string with a new character(s). Actually, it does not replace the specific character(s) in the string, instead returns the new string because the string is immutable.
text = 'Hey, how ade you?'
text.replace('d','r')
text = 'Hey, how are you?'
text.replace('Hey','Hello')
Passing the count as 2, will replace only the first 2 occurrences of ‘Hello’.
text = 'Hello, Hello, Hello.'
text.replace('Hello','Hey',2)
Python String rstrip()
Method
Remove the right whitespace(s) from the string. By default, it will strip the whitespace(s), but we can pass the substring to trim.
text = ' how are you? '
text.rstrip()
text = ' how are you?'
text.rstrip('you?')
Python String split()
Method
It will try to split the string based on the separator. By default, space(”) is the separator and the separator is removed from the string.
We also have another parameter for maxsplit, i.e. the maximum number of splits we want to make. For example, if we wish to split a string into two strings, then we can specify maxsplits as 1.
text = 'Hello! Peter Cruise'
text.split()
#we want to split based on '!'
text.split('!')
text.split('e',1)
Python String startswith()
Method
To check if a string starts with a specified character(s). Returns True if the string starts with a specified character(s), else False.
name = 'Will Smith'
name.startswith('w')
name = 'Will Smith'
name.startswith('W')
Python String strip()
Method
Remove whitespace(s) from both sides of the string. We can pass the substring as an argument, and it will compare each character of the substring with each character of the string and remove it.
text = ' how are you? '
text
text.strip()
It can strip the characters in the substring from the sting. Comparing character by character, not whole substring at once.
text = 'Hey, how are you?'
text.strip('Hesd?you, ')
Python String swapcase()
Method
Swaps the lowercase character to upper and uppercase to lower.
text = 'how are you?'
text.swapcase()
text = 'How are you?'
text.swapcase()
Python String title()
Method
The first character of each word is converted to upper case. If any of the remaining characters are upper case, they would be converted to lower case.
text = 'how arE you?'
text.title()
Python String upper()
Method
To convert all characters of the string to uppercase.
name = 'Peter Cruise'
name.upper()