Python String Methods With Examples
What are String Methods in Python?
With the string object, you may use a variety of string methods in Python. One of these is the format() function, which we discussed before.
Lower(), upper(), join(), split(), find()
, and replace()
are some of the most regularly used techniques. Here's a comprehensive list of Python's built-in techniques for working with strings.
Example:
print( "wScUbEtEcH".lower())
Output:
Code:
print("wScUbEtEcH".upper())
Output:
Split String
The split() function produces a list in which the text between the separators is used as the list elements.
Code:
a = "welcome to wscube"
print(a.split(" "))
Output:
'This will join all words into a string
Code:
print( 'Happy New Year'.find('ew'))
Output:
-
capitalize()
The initial character is converted to uppercase.
Code:
text = "welcome to wscube"
x = text.capitalize()
print (x)
Output:
-
casefold()
Converts string into lower case.
Code:
text = "Welcome To Wscube"
x = text.casefold()
print (x)
Output:
-
center()
Returns a centered string.
Code:
text = "wscube"
x = text.center(10)
print(x)
Output:
-
count()
This function returns the frequency of times a given value appears in a string.
Code:
text = "welcome to wscube"
x = text.count("w")
print(x)
Output:
-
encode()
Returns a string that has been encoded.
Code:
text = "My name is Ståle"
x = text.encode()
print(x)
Output:
-
endswith()
If the string terminates with the supplied value, this function returns true.
Code:
text = "welcome to wscube"
x = text.endswith("e")
print(x)
Output:
-
expandtabs()
Sets the string's tab size.
Code:
text = "w\ts\tc\tu\tb\te"
x = text.expandtabs(2)
print(x)
Output:
-
index()
Returns the location of where a supplied value was discovered after searching the string for it.
Code:
text = "welcome to wscube"
x = text.index("to")
print(x)
Output:
-
isalnum()
If all of the items of a string are alphanumeric, this function returns True.
Code:
text = "wscube123"
x = text.isalnum()
print(x)
Output:
-
isalpha()
Returns True if all of the items in the string are alphabetical.
Code:
text = "wscube"
x = text.isalpha()
print(x)
Output:
-
isdecimal()
In case all the characters in the string are decimals, this function returns True.
Code:
text = "\u0033"
x = text.isdecimal()
print(x)
Output:
-
isdigit():
If the string contains only digits, it returns true.
Code:
text = "12345"
x = text.isdigit()
print(x)
Output:
-
isidentifier()
It shows the output as True if the string is an identifier
If the string is a legitimate identifier, the isidentifier() function returns True, otherwise False.
If a string comprises solely alphanumeric letters (a-z) and numbers (0-9) or underscores (_), it is considered a valid identification. A valid identification can’t contain any spaces or begin with a number.
Code:
text = "wscube"
x = text.isidentifier()
print(x)
Output:
-
isnumeric()
Returns True if the string contains just numeric characters.
If all of the characters are numeric (0-9), the isnumeric() function returns True, otherwise False.
Exponents such as 2 and 34 are likewise regarded as numeric values. Because all of the characters in the string must be numeric, and the - and the. are not, "-1" and "1.5" are not considered numeric values.
Code:
text = "56"
x = text.isnumeric()
print(x)
Output:
-
isprintable()
If all of the items in the string are printable, this function returns True.
The isprintable()
method returns True if all the characters are printable, otherwise False.
Code:
text = "wscube"
x = text.isprintable()
print(x)
Output:
-
isspace()
If all of the characters in the string are whitespaces, this function returns True.
Code:
text = " "
x = text.isspace()
print(x)
Output:
-
istitle()
If the string follows the rules of a title, it returns true.
Code:
text = "Welcome To Wscube"
x = text.istitle()
print(x)
Output:
-
join()
Connects the components of an iterable to the string's end.
Code:
demo = ("welcome", "to", "wscube")
x = "#".join(demo)
print(x)
Output:
-
ljust()
This method returns a string that is left justified.
The ljust()
method will left-align the string, using a specified character (space is default) as the fill character.
Code:
text = "welcome"
x = text.ljust(10)
print(x, "to wscube")
Output:
-
islower()
Returns True if all items in the string are lower case.
Code:
text = "wscube tech"
x = text.islower()
print(x)
Output:
-
isupper()
Returns True if all items in the string are upper case.
Code:
text = "WSCUBE"
x = text.isupper()
print(x)
Output:
-
lstrip()
Gives a left trim version of the string.
Code:
text = " mango "
x = text.lstrip()
print("of all fruits", x, "is my favorite")
Output:
-
maketrans()
Gives a translation table to be used in translations.
Code:
text = "Hello Saam"
mytable = text.maketrans("S", "P")
print(text.translate(mytable))
Output:
-
partition()
Gives a tuple where the string is divided into three parts.
Code:
text = "I could eat mangos all day"
x = text.partition("mangos")
print(x)
Output:
-
rfind()
Looks for the string for a specified value and returns the last position of where it was found.
Code:
text = "wscube"
x = text.rfind("e")
print(x)
Output:
-
rindex()
Looks for the string for a specified value and returns the last position of where it was found.
Code:
text = "wscube"
x = text.rindex("casa")
print(x)
Output:
-
rjust()
Gives a right justified version of the string.
Code:
text = "banana"
x = text.rjust(10)
print(x, "is my favorite fruit.")
Output:
-
rsplit()
Divides the string in two at the specified separator, and returns a list.
Code:
text = "wscube, jodhpur, rajasthan"
x = text.rsplit(", ")
print(x)
Output:
-
rstrip()
Gives a right trim version of the string.
Code:
text = " mango "
x = text.rstrip()
print("of all fruits", x, "is my favorite")
Output:
-
split()
Divides the string in two at the specified separator, and returns a list.
Code:
text = "wscube, jodhpur, rajasthan"
x = text.split(", ")
print(x)
Output:
-
splitlines()
Divides the string in two at line breaks and returns a list.
Code:
text = "Thank you\nWelcome to wscube"
x = text.splitlines()
print(x)
Output:
-
startswith()
Gives True if the string starts with the specified value.
Code:
text = "Hello, welcome to wscube"
x = text.startswith("Hello")
print(x)
Output:
-
strip()
Gives a trimmed version of the string.
Code:
text = " mango "
x = text.strip()
print("of all fruits", x, "is my favorite")
Output:
-
swapcase()
upper case becomes lower case and vice versa.
Code:
text = "Hello Welcome To WSCUBE"
x = text.swapcase()
print(x)
Output:
-
zfill()
At the start of the string, it is filled with a predetermined number of 0 values.
Code:
text = "40"
x = text.zfill(10)
print(x)
Output: