You are currently viewing 40 Python String Methods You Should Know

40 Python String Methods You Should Know

Strings are an integral part of your programs, but they can be tricky to work with. Thankfully, Python offers many string methods to help make dealing with strings as painless as possible. Whether you’re dealing with the basics of strings or looking at the more advanced functions, these Python string methods will help you get the job done quickly and efficiently. In this blog, you will learn 40 important python string methods. Let’s start.

String capitalize() Method

In the capitalize() method, only the first character of a string is capitalized.

txt = "hi world, this is ai octa."
x = txt.capitalize()
print(x)

Output:

Hi world, this is ai octa.

String casefold() Method

If a string contains case distinctions, the casefold() method removes them.

txt = "HI mIß, THIS IS AI OCTA."
x = txt.casefold()
print(x)

Output:

hi miss, this is ai octa.

String lower() Method

In the lower() method, the string is returned in lowercase.

txt = "Hi WORLD, This is AI Octa."
x = txt.lower()
print(x)

Output:

hi world, this is ai octa.

String swapcase() Method

A string returned by swapcase() has all uppercase letters in lowercase and vice versa.

txt = "Hi WORLD, this is ai octa."
x = txt.swapcase()
print(x)

Output:

hI world, THIS IS AI OCTA.

String title() Method

The title() method produces strings where the first letter in each word is capitalized.

txt = "hi world, this is ai octa."
x = txt.title()
print(x)

Output:

Hi World, This Is Ai Octa.

String upper() Method

Using the upper() method, you can output a string in all capital letters.

txt = "Hi World, this is ai octa."
x = txt.upper()
print(x)

Output:

HI WORLD, THIS IS AI OCTA.

String encode() Method

The encode() method uses the specified encoding on the given string and uses UTF-8 if none is specified.

txt = "hi world, this is ai octa."
x = txt.encode()
print(x)

Output:

b'hi world, this is ai octa.'

String isalnum() Method

If a string is made only of alphabetic and numeric characters, isalnum() will return True.

txt = "67348AiOcta"
x = txt.isalnum()
print(x)

Output:

True

String isalpha() Method

When all characters in a string are in the alphabet, the isalpha() method returns True.

txt = "67348AiOcta"
x = txt.isalpha()
print(x)

Output:

False

String isdecimal() Method

If all characters in the string are decimals, isdecimal() returns True.

txt = "\u00B2"
x = txt.isdecimal()
print(x)

Output:

False

String isdigit() Method

If all characters in the string are digits, isdigit() returns True.

txt = "\u00B2"
x = txt.isdigit()
print(x)

Output:

True

String isidentifier() Method

A string that is used as an identifier returns True. An identifier may refer to a variable, function, class, module, or other things.

txt = "AiOcta"
x = txt.isidentifier()
print(x)

Output:

True

String isnumeric() Method

Isnumeric() returns True if all characters in the string are numeric, such as VIII, 9, or 2.

txt = "Ⅷ"
x = txt.isnumeric()
print(x)

Output:

True

String isprintable() Method

The isprintable() method is satisfied when all characters in the string are printable.

txt = "\n"
x = txt.isprintable()
print(x)

Output:

False

String isspace() Method

If all characters in the string are whitespaces, isspace() returns True.

txt = "    "
x = txt.isspace()
print(x)

Output:

True

String istitle() Method

The istitle() method determines whether a string follows the rules for a title.

txt = "Hi World"
x = txt.istitle()
print(x)

Output:

True

String isupper() Method

If all characters in the string are upper case, the isupper() method returns True.

txt = "Hi World, this is ai octa."
x = txt.isupper()
print(x)

Output:

False

String islower() Method

This method determines if every character in the string is lowercase.

txt = "hi world, this is ai octa."
x = txt.islower()
print(x)

Output:

True

String center() Method

The center() method aligns a string in the center of the container with a space character as the separator.

txt = "hi world, this is ai octa."
x = txt.center(50)
print(x)

Output:

            hi world, this is ai octa.           

String ljust() Method

The ljust() method uses a specified fill character (space is the default) to left align the string.

txt = "hi world, this is ai octa."
x = txt.ljust(50)
print(x)

Output:

hi world, this is ai octa.                        

String rjust() Method

Using rjust(), the string will be right aligned, with space acting as the fill character.

txt = "hi world, this is ai octa."
x = txt.rjust(50)
print(x)

Output:

                        hi world, this is ai octa.

String strip() Method

The strip() method will delete all leading and trailing spaces.

txt = "    hi world, this is ai octa.    "
x = txt.strip()
print(x)

Output:

hi world, this is ai octa.

String lstrip() Method

Strings are left-trimmed using the lstrip() method.

txt = "       hi world, this is ai octa."
x = txt.lstrip()
print(x)

Output:

hi world, this is ai octa.

String rstrip() Method

The rstrip() method deletes the string’s tail and space is the default tail character to delete.

txt = "hi world, this is ai octa.     "
x = txt.rstrip()
print(x)

Output:

hi world, this is ai octa.

String index() Method

The index() method retrieves the first occurrence of a specified value.

txt = "hi world, this is ai octa."
x = txt.index("i")
print(x)

Output:

1

String rindex() Method

rindex() searches a string for a specific value and returns the location of the string’s occurrence in the string.

txt = "hi world, this is ai octa."
x = txt.rindex("i")
print(x)

Output:

19

String find() Method

The find() method locates the first match for the given parameter.

txt = "hi world, this is ai octa."
x = txt.find("i")
print(x)

Output:

1

String rfind() Method

Using the rfind() method you can search for a specified value and the last place it is found will be returned.

txt = "hi world, this is ai octa."
x = txt.rfind("i")
print(x)

Output:

19

String startswith() Method

If the string starts with the specified value, the startswith() method returns True.

txt = "hi world, this is ai octa."
x = txt.startswith("hi world")
print(x)

Output:

True

String endswith() Method

Using the endswith() method, it is determined if the string ends with the desired value. If it does not, False is returned, otherwise, True.

txt = "hi world, this is ai octa."
x = txt.endswith("octa.")
print(x)

Output:

True

String split() Method

The split() method splits the string at the specified separator and returns a list.

txt = "hi world, this is aiocta."
x = txt.split("i")
print(x)

Output:

['h', ' world, th', 's ', 's a', 'octa.']

String rsplit() Method

There is only one difference between rsplit() and split(): rsplit() splits the string on the right.

txt = "hi world, this is ai octa."
x = txt.rsplit(", ")
print(x)

Output:

['hi world', 'this is ai octa.']

String splitlines() Method

A list is returned by the splitlines() method after the string has been split at line breaks.

txt = "hi world, \nthis is ai octa."
x = txt.splitlines()
print(x)

Output:

['hi world, ', 'this is ai octa.']

String partition() Method

Splitting a string into a tuple containing three elements is performed by the partition() method.

txt = "hi world, this is ai octa."
x = txt.partition("i")
print(x)

Output:

('h', 'i', ' world, this is ai octa.')

String rpartition() Method

The rpartition() method finds the last occurrence of a specified string and creates a tuple containing three elements.

txt = "hi world, this is ai octa."
x = txt.rpartition("i")
print(x)

Output:

('hi world, this is a', 'i', ' octa.')

String count() Method

In a string, count() returns how many times a specific value occurs.

txt = "hi world, this is ai octa."
x = txt.count("i")
print(x)

Output:

4

String expandtabs() Method

String tab sizes can be set using the expandtabs() method.

txt = "hi\tworld, this is ai octa."
x = txt.expandtabs()
print(x)

Output:

hi      world, this is ai octa.

String join() Method

The join() python string method turns a sequence into a string, joining each member with a string.

myTuple = ("hi", "world", "this", "is", "aiocta.")
x = " ".join(myTuple)
print(x)

Output:

hi world this is aiocta.

String replace() Method

The replace() method substitutes a specific phrase with another specific phrase.

txt = "hi world, this is ai octa."
x = txt.replace("i", "o")
print(x)

Output:

ho world, thos os ao octa.

String zfill() Method

The zfill() method starts with the zero-position of the input string and adds zeros (0) until it reaches the desired length.

txt = "hi world, this is ai octa."
x = txt.zfill(50)
print(x)

Output:

000000000000000000000000hi world, this is ai octa.

Leave a Reply