Skip to content
Home » Blog » Top 24 Python String Methods now in a Simplified Version

Top 24 Python String Methods now in a Simplified Version

Python String Methods

Working with Strings is one of the most important things to be learned and understood in python. Today, we’ll be discussing 24 built-in Python String Methods and how you can use them effectively.

Python String Methods

To make it easier for you we have displayed it under 4 headings

  1. Case-Based
  2. Strings and Lists
  3. Checking the Characters
  4. Editing the Strings

Case-Based Python String Methods

Using the 8 String methods mentioned below you can manipulate a string based on its case. In addition, Some of these can also be used to check the case of the String, if it is lower case, upper case, capitalized, or so on.

x = "The hand sanitizer was actually clear glue."




#1 UPPER()

print(x.upper())    #The hand sanitizer was actually clear glue.
#upper() is used to convert the whole string to uppercase



#2 LOWER()

print(x.lower())    #the hand sanitizer was actually clear glue.
#lower() is used to convert the whole string to lowercase



#3 ISUPPER()

print(x.isupper())    #False
#isupper() is used to check if the whole string is in uppercase



#4 ISLOWER()

print(x.islower())    #False
#islower() is used to check if the whole string is in lowercase



#5 TITLE()

print(x.title())    #The Hand Sanitizer Was Actually Clear Glue.
#title() converts the first letter of every word to uppercase, and the rest to lowercase



#6 CAPITALIZE()

print(x.capitalize())    #The hand sanitizer was actually clear glue.
#capitalize() converts the first letter of every sentence to uppercase, and the rest to lowercase



#7 ISTITLE()

print(x.istitle())    #False
#istitle checks if the string is in title case



#8 SWAPCASE()

print(x.swapcase())    #tHE HAND SANITIZER WAS ACTUALLY CLEAR GLUE.
#swapcase() is used to swap the cases in the string

With that, we’re done with all 8 case-based python string methods, pretty straightforward and easy to understand, all you need is a bit of practice to get familiar.

STRINGS AND LISTS

The next 4 String Methods that we are gonna discuss, manipulate the process of converting a String to a list or the other way around

x = "Pigs must be \n able to fly \n in Hog Heaven."
y = x.split()




#9 SPLIT()

print(x.split())    #['Pigs', 'must', 'be', 'able', 'to', 'fly', 'in', 'Hog', 'Heaven.']
#The split() method splits a string into a list

print(x.split('e')) #['Pigs must b', ' \n abl', ' to fly \n in Hog H', 'av', 'n.']
#You can specify the separator, default separator is whitespace.



#10 SPLITLINES()

print(x.splitlines())    #['Pigs must be ', ' able to fly ', ' in Hog Heaven.']
#Splits the string at line breaks ("\n")



#11 PARTITION()

print(x.partition('be')) #('Pigs must ', 'be', ' \n able to fly \n in Hog Heaven.')
#Splits a string into 3 parts and returns a tuple



#12 JOIN()

print(''.join(y))        #PigsmustbeabletoflyinHogHeaven.
#Joins elements of an iterable into a string

Now we’re halfway through the article, if you are able to remember all those previously discussed string methods, that’s great, if you’re not, that’s totally fine, you can always practice them.

With that said, let’s move on to the next set of methods when you’re ready.

CHECKING characters IN STRING

Under this category, we check the type of characters in the string using another bunch of string methods

x = "p455w0rd"




#13 ISALNUM()

print(x.isalnum())    #True
#Checks if the string is alpha numeric



#14 ISALPHA

print(x.isalpha())    #False
#Checks if all characters in the string are alphabets, here, "9" is not an alphabet, therefore False



#15 ISDIGIT

print(x.isdigit())    #False
#Checks if all characters in the string are digits



#16 ISSPACE

print(x.isspace())    #False
#Checks if all characters in the string are spaces



#17 STARTSWITH

print(x.startswith('p4'))  #True
#Returns True if the string starts with a specific value



#18 ENDSWITH

print(x.endswith('rd'))   #True
#Returns True if the string ends with a specific value



#19 FIND

print(x.find('w'))    #4
#Returns the index of the specific value, if not found, returns -1



#20 INDEX

print(x.index('w'))    #4
#Same as the find() string method, but unlike find, index raises an error if the value is not found

EDITING THE STRINGS

These methods can be used to modify a string quickly and can come in real handy when removing whitespaces or replacing characters in a string

x = "    full    "



#21 STRIP()

print(x.strip())    #'full'
#Removes all the whitespaces in a string



#22 LSTRIP()

print(x.lstrip())    #'full    '
#Removes all the whitespaces at the left side of a string



#23 RSTRIP()

print(x.rstrip())    #'    full'
#Removes all the whitespaces at the right side of a string



#24 REPLACE()

print(x.replace('ul', 'oo'))    #'    fool    '
#Replaces a specific value with a specific value

TO SUM UP . . .

In today’s post, we’ve discussed 24 String Methods in Python and how to use them. With a little bit of practice, you should be able to get a hang of it 

If you are on your way to becoming a Pythonista, you might find our site really helpful. Hit this link for more python related posts


Check it out, it’s Simply the Best Code.

Leave a Reply

Your email address will not be published. Required fields are marked *