Python Tutorial for Beginners

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:

'Wscubetech'

Code

print("wScUbEtEcH".upper())

Output:

'WSCUBETECH'

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:

[“"welcome”, “to”, “wscube"]

'This will join all words into a string

Code:

print( 'Happy New Year'.find('ew'))

Output:

7
  • capitalize()

The initial character is converted to uppercase.

Code:

text = "welcome to wscube"

x = text.capitalize()

print (x)

Output:

Welcome to wscube
  • casefold()

Converts string into lower case.

Code:

text = "Welcome To Wscube"

x = text.casefold()

print (x)

Output:

welcome to wscube

https://youtu.be/tUBJeSyzuG4

  • center()

Returns a centered string.

Code:

text = "wscube"

x = text.center(10)

print(x)

Output:

wscube
  • 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:

2
  • encode()

Returns a string that has been encoded.

Code:

text = "My name is Ståle"

x = text.encode()

print(x)

Output:

b'My name is St\xc3\xe5le'
  • 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:

True
  • expandtabs()

Sets the string's tab size.

Code:

text = "w\ts\tc\tu\tb\te"

x =  text.expandtabs(2)

print(x)

Output:

w s c u b e
  • 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:

8
  • isalnum()

If all of the items of a string are alphanumeric, this function returns True.

Code:

text = "wscube123"

x = text.isalnum()

print(x)

Output:

True
  • isalpha()

Returns True if all of the items in the string are alphabetical.

Code:

text = "wscube"

x = text.isalpha()

print(x)

Output:

True
  • isdecimal()

In case all the characters in the string are decimals, this function returns True.

Code:

text = "\u0033" 

x = text.isdecimal()

print(x)

Output:

True
  • isdigit(): 

If the string contains only digits, it returns true.

Code:

text = "12345"

x = text.isdigit()

print(x)

Output:

True

https://youtu.be/fHXG7J-7UMk

  • 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:

True 
  • 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:

True
  • 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:

True
  • isspace()

If all of the characters in the string are whitespaces, this function returns True.

Code:

text = "   "

x = text.isspace()

print(x)

Output:

True
  • istitle()

If the string follows the rules of a title, it returns true.

Code:

text = "Welcome To Wscube"

x = text.istitle()

print(x)

Output:

True
  • join()

Connects the components of an iterable to the string's end.

Code:

demo = ("welcome", "to", "wscube")

x = "#".join(demo)

print(x)

Output:

welcome#to#wscube
  • 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:

welcome      to wscube
  • islower()

Returns True if all items in the string are lower case.

Code:

text = "wscube tech"

x = text.islower()

print(x)

Output:

True
  • isupper()

Returns True if all items in the string are upper case.

Code:

text = "WSCUBE"

x = text.isupper()

print(x)

Output:

True
  • lstrip()

Gives a left trim version of the string.

Code:

text = "     mango     "

x = text.lstrip()

print("of all fruits", x, "is my favorite")

Output:

of all fruits mango     is my favorite
  • maketrans()

Gives a translation table to be used in translations.

Code:

text = "Hello Saam"

mytable = text.maketrans("S", "P")

print(text.translate(mytable))

Output:

Hello Paam
  • 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:

('I could eat ', 'mangos', ' all day')
  • 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:

5
  • 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:

5
  • rjust()

Gives a right justified version of the string.

Code:

text = "banana"

x = text.rjust(10)

print(x, "is my favorite fruit.")

Output:

              banana is my favorite fruit.
  • 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:

['wscube', 'jodhpur', 'rajasthan']
  • rstrip()

Gives a right trim version of the string.

Code:

text = "     mango     "

x = text.rstrip()

print("of all fruits", x, "is my favorite")

Output:

of all fruits     mango is my favorite
  • 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:

['wscube', 'jodhpur', 'rajasthan']
  • 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:

['Thank you ', 'Welcome to wscube']
  • startswith()

Gives True if the string starts with the specified value.

Code:

text = "Hello, welcome to wscube"

x = text.startswith("Hello")

print(x)

Output:

True
  • strip()

Gives a trimmed version of the string.

Code:

text = "     mango     "

x = text.strip()

print("of all fruits", x, "is my favorite")

Output:

of all fruits     mango is my favorite
  • swapcase()

upper case becomes lower case and vice versa.

Code:

text = "Hello Welcome To WSCUBE"

x = text.swapcase()

print(x)

Output:

hELLO wELCOME tO wscube
  • 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:

0000000040

https://youtu.be/PUAGiw3ZjAg

Did you find this article helpful?