Enable Dark Mode!
how-to-use-common-python-string-methods.jpg
By: Aswani VT

How to Use Common Python String Methods

Technical

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,
Eg:
x = “My World”
print(x[1])
output: y
The length of the string can be achieved by using the len() function.
E.g.:
x= “My World”
print(len(x))
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.
Eg:
x = "MY WORLD."
y = x.capitalize()
print (y)
output:  my world.
3. center()
The function returns a centered string.
Eg:
We can use ‘p as the padding character.
x = "carrot"
y = x.center(20, "P")
print(y)
Output: PPPPPPPcarrotPPPPPPP
4. count()
This function returns the number of occurrences of a specified value.
Eg:
x = "I love bananas, banana are my favorite fruit"
y = x.count("banana")
print(y) 
output:
5. encode()
This function returns an encoded string.
Eg:
x = "My name is John"
y = x.encode()
print(y) 
output:  b'My name is John'
6. endswith()
This function returns true if the string endswith given value.
Eg:
x = "My name is John."
y = x.endswith(".")
print(y)
output: True
7. expandtabs()
This function sets the tab size of the string.
Eg:
x = "W\to\tr\tl\td"
y =  x.expandtabs(4)
print(y)
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.
Eg:
x = "My name is John."
y = x.find("name")
print(y) 
output: 3
9. format()
This function formats the given value in the string.
Eg:
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.
Eg:
x = "My world"
y = x.index("world")
print(y)
output: 3
25. join()
This function joins the elements of an iterable into a single string.
Eg:
x = ("a", "b", "c")
y = "#".join(x)
print(y)
output: a#b#c
26.ljust()
This function returns the left justified version of given text.
Eg:
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.
Eg:
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.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message