-
lower()
Converts all uppercase characters in a string into lowercase.
my_str="Hello World" print(my_str.lower()) # hello world print(my_str) # Hello World
-
islower()
Checks if all characters in the string are lowercase.
str = 'Hello World' print(str.islower()) # False str = 'hello world' print(str.islower()) # True
-
casefold()
Returns a string where all the characters are lower case.
my_str="HeLLo World" print(my_str.casefold()) # hello world
-
upper()
Converts all lowercase characters in a string into uppercase.
my_str="Hello World" print(my_str.upper()) # HELLO WORLD print(my_str) # Hello World
-
isupper()
Checks if all characters in the string are uppercase.
str = 'Hello World' print(str.isupper()) # False str = 'HELLO' print(str.isupper()) # True
-
title()
Convert string to title case
my_str="hello world" print(my_str.title()) # Hello World
-
istitle()
Returns “True” if the string is a title cased string.
str = 'Hello World' print(str.istitle()) # True str = 'hello world' print(str.istitle()) # False
-
capitalize()
Convert the first character of a string to capital (uppercase) letter.
my_str="hello world" print(my_str.capitalize()) # Hello world
-
swapcase()
Swap the cases of all characters in a string.
my_str="Hello World" print(my_str.swapcase()) # hELLO wORLD
-
center()
Pad the string with the specified character upto specified length.
my_str="Hello World" print(my_str.center(16,'a')) # aaHello Worldaaa print(my_str.center(20,'a')) # aaaaHello Worldaaaaa
-
count()
Returns the number of occurrences of a substring in the string.
my_str="Hello World Hello Hello" print(my_str.count("Hello")) # 3 print(my_str.count('l')) # 7
-
encode()
Encodes strings with the specified encoded scheme.
In case of failure, it raises a UnicodeDecodeError exception.
my_str="Hello World" # change encoding to utf-8 print(my_str.encode()) # b'Hello World'
-
endswith()
Returns “True” if a string ends with the given suffix.
my_str="Hello World" print(my_str.endswith("World")) # True
-
expandtabs()
Specifies the amount of space to be substituted with the “\t” symbol in the string. Default tabsize is 8.
str = 'Hello\tWorld' print(str.expandtabs()) # Hello World print(str.expandtabs(20)) # Hello World
-
find()
Returns the lowest index of the substring if it is found.
str = 'Hello World' print(str.find("World")) # 6 print(str.find("Galaxy")) # -1
-
index()
Returns the position of the first occurrence of a substring in a string.
If substring doesn't exist inside the string, it raises a ValueError exception.
str = 'Hello World' print(str.index("World")) # 6
-
format()
Formats the string for printing it to console.
name="Mike" str = 'Hello {}!' print(str.format(name)) # Hello Mike! # We can also do it this way str2= f"Hello {name}!" print(str2) # Hello Mike!
-
format_map()
Formats specified values in a string using a dictionary.
coordinates = {'x':4,'y':-5, 'z': 0} print('{x} {y} {z}'.format_map(coordinates)) # 4 -5 0
-
isalnum()
Checks whether all the characters in a given string is alphanumeric or not.
str = 'Hello World' print(str.isalnum()) # False str = 'HelloWorld' print(str.isalnum()) # True str = 'HelloWorld1100' print(str.isalnum()) # True
-
isalpha()
Returns “True” if all characters in the string are alphabets.
str = 'Hello World' print(str.isalpha()) # False str = 'HelloWorld' print(str.isalpha()) # True str = 'HelloWorld1100' print(str.isalpha()) # False
-
isnumeric()
Returns “True” if all characters in the string are numeric characters.
str = 'Hello World' print(str.isnumeric()) # False str = '12131' print(str.isnumeric()) # True
-
isdecimal()
Returns true if all characters in a string are decimal.
str = 'Hello World' print(str.isdecimal()) # False str = '1234567' print(str.isdecimal()) # True str = '9866asdf3s' print(str.isdecimal()) # False
-
isdigit()
Returns “True” if all characters in the string are digits.
str = '1234567' print(str.isdigit()) # True str = '9866asdf3s' print(str.isdigit()) # False
-
isidentifier()
The isidentifier() method returns True if the string is a valid identifier in Python.
str = 'Hello World' print(str.isidentifier()) # False str = 'while' print(str.isidentifier()) # True str = 'Python' print(str.isidentifier()) # True
-
isprintable()
Returns “True” if all characters in the string are printable or the string is empty.
str = 'Hello World' print(str.isprintable()) # True str = 'Hello World\n' print(str.isprintable()) # False
-
isspace()
Returns “True” if all characters in the string are whitespace characters.
s = ' \t' print(s.isspace()) # True s = ' a ' print(s.isspace()) # False s = '' print(s.isspace()) # False
-
join()
The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.
text = ['Python', 'is', 'a', 'fun', 'programming', 'language'] print(' '.join(text)) # Python is a fun programming language print('_'.join(text)) # Python_is_a_fun_programming_language
-
ljust()
Left aligns the string according to the width specified
-
lstrip()
Returns the string with leading characters removed
-
maketrans()
Returns a translation table
-
partition()
Splits the string at the first occurrence of the separator
-
replace()
Replaces all occurrences of a substring with another substring
-
rfind()
Returns the highest index of the substring
-
rindex()
Returns the highest index of the substring inside the string
-
rjust()
Right aligns the string according to the width specified
-
rpartition()
Split the given string into three parts
-
rsplit()
Split the string from the right by the specified separator
-
rstrip()
Removes trailing characters
-
splitlines()
Split the lines at line boundaries
-
startswith()
Returns “True” if a string starts with the given prefix
-
strip()
Returns the string with both leading and trailing characters
-
translate()
Modify string according to given translation mappings
-
zfill()
Returns a copy of the string with ‘0’ characters padded to the left side of the string
Reference
Top comments (0)