Enable Dark Mode!
By: Aswani VT

How to Use Common Python String Methods


Python string methods are some set of built-in functions that can be used on strings. 
Python strings are enclosed by either single or double quotation marks. 
That is, ‘My world’ is the same as “My world.” One can display python strings using the print() function. Multiline strings are assigned using triple quotes.
Python strings are represented using Unicode characters which are actually arrays of bytes. Thus we can loop/iterate through the characters of a string using a  “for”  loop. In Order to access an element of a string, we can use square brackets as below,
x = “My World”
output: y
The length of the string can be achieved by using the len() function.
x= “My World”
output: 8
Python string methods return new values, and the original string remains unchanged.
Let’s see some of the common built-in methods in python with examples, 
1. capitalize()
The First letter of the string is capitalized.
x = "my world."
y = x.capitalize()
print (y)
output:  My world.
2. casefold()
String is converted to lowercase.
x = "MY WORLD."
y = x.capitalize()
print (y)
output:  my world.
3. center()
The function returns a centered string.
We can use ‘p as the padding character.
x = "carrot"
y = x.center(20, "P")
4. count()
This function returns the number of occurrences of a specified value.
x = "I love bananas, banana are my favorite fruit"
y = x.count("banana")
5. encode()
This function returns an encoded string.
x = "My name is John"
y = x.encode()
output:  b'My name is John'
6. endswith()
This function returns true if the string endswith given value.
x = "My name is John."
y = x.endswith(".")
output: True
7. expandtabs()
This function sets the tab size of the string.
x = "W\to\tr\tl\td"
y =  x.expandtabs(4)
output: W   o   r   l   d
8. find()
This function searches the given value in the string and returns the position of the given value.
x = "My name is John."
y = x.find("name")
output: 3
9. format()
This function formats the given value in the string.
x = "I have {num:.2f} rupees!"
print(x.format(num = 20))
output: I have 20.00 rupees!

10isalnum()This function checks if all characters are alphanumeric and return true.
11isalpha()This function checks if all characters are in the alphabet and return true.
12isascii() This function checks if all characters are ASCII and return true.
13isdecimal()This function checks if all characters are digits and return true.
14isidentifier()This function checks if the string is an identifier and returns true.
15islower()This function checks if all characters are lowercase and return true.
16isnumeric()This function checks if all characters are numeric and return true.
17isprintable()This function checks if all characters are printable and return true.
18isspace()This function checks if all characters are whitespaces and return true.
19istitle()This function checks if the string follows the rules of a title and returns true.
20isupper()This function checks if all characters are uppercase and return true.
21lower()This function converts all characters of the string into lowercase.
22upper()This function converts all characters of the string into uppercase.
23title()This function converts the first character of each word in the string into uppercase.
24. index()
This function returns the position of the specified value.
x = "My world"
y = x.index("world")
output: 3
25. join()
This function joins the elements of an iterable into a single string.
x = ("a", "b", "c")
y = "#".join(x)
output: a#b#c
This function returns the left justified version of given text.
x = "Life"
y = x.ljust(10)
print(y, "is amazing.")
output: Life       is amazing.
27. lstrip()
This function returns a left-striped version of a given string.
x = " life "
y = x.lstrip()
print("My", y ,"is beautiful")
output: My is beautiful
28maketrans()Used with the translate method to replace specified characters by forming a mapping.
29partition()Searches for a given element and parts the string into three.
30replace()The given value is replaced with the specified character
31rfind()Returns the last position of the specified value.
32rindex()Returns the last position of the specified value.
33rjust()This function returns the right justified version of the given text.
34rstrip()This function returns a right-striped version of a given string.
35split()The given string is split at a specified value and returned as a list
36splitlines()The splitting occurs at line breaks
37startswith()This function returns True if a string starts with the given value
38strip()A trimmed version of the string is returned
39swapcase()Lowercase letters converted into upper and uppercase content in lower
40translate()The translated string is returned
41zfill()Fills the string with a given number of zeros at the starting

Above are the most common python string methods with their usage. Learn more about them by trying out each function.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message