Python String Methods
By: Binamin Hussein

Python String Methods

Python String Methods.

In python, a method is a function that is available for a given object because of the object's type. Strings are objects and they have methods. In this article i will be going through some of the built-in string method


casefold()

str.casefold() creates a lower case string that is suitable for case insensitive comparisons. It is more aggressive than str.lower() and may modify strings that are already in lower case or cause string to grow in length and is not intended for display.


ABD'.casefold()     #abd

"XßΣ".casefold()   # xssσ
        

upper()

str.upper() takes every character in a string and converts it to its uppercase equivalent.

'This is a string'.upper()  #THIS IS A STRING
        

lower()

str.lower() takes every character of the string and converts it to its lowercase equivalent.

'This Is a STRing'.lower()  #this is a string        

capitalize()

str.capitalize() returns a capitalized version of the string. It makes the 1st character of the string uppercase, the rest remain to lower case.

'this is a string'  #This is a string        

title()

str.title() returns the title cased version of a string. Every letter at the beginning of every word is converted to upper case, the rest are made lower case.

 'this is a string'.title()  #This Is a String        

swapcase()

str.swapcase() returns a new string object in which all lowercase characters are converted to uppercase and all uppercase characters are converted to lowercase.

'tHiS is A StrIng'.swapcase()  #ThIs IS a sTRiNG        

translate()

str.translate() allows you to specify a translation table( used for replacements ) as well as any character which should be deleted in the process. You first need to generate a translation table.

 # syntax
str.translate(table[,deletechars])

## generating a translation table

translation_table = str.maketrans('aeiou','12345')

# the variable
my_string = 'This is a string'

# translate

my_string.translate(translation_table) #Th3s 3s 1 str3ng        

The translate method returns a string which is a translated copy of the original string.

You can set the table to None if you only need to delete characters.

'this is a string'.translate(None, 'aeiou') # ths s strng        

format()

Python provides string interpolation and formatting through str.format().

'I am from {}. I love cupcakes from {}.".format('Australia','Kenya') 
 

 # I am from Australia. I love cupcakes from Kenya.        

strip()

str.strip() returns a new string object with unwanted characters removed.

">>> a python prompt".strip('> ') #a python prompt        

an empty strip() is used to remove white-space

"   a line with leading and trailing space    ".strip()
#a line with leading and trailing space        

rstrip() and lstrip()

They strip a string with direction. str.rstrip() starts from the end of the string while str.lstrip() splits from the start of the string.

## rstrip
'  spacious string  '.rstrip() #'  spacious string'

## lstrip
'  spacious string   '.lstrip() #'spacious string  '        

split()

str.split() takes a string a returns a list of sub-strings of the original string.

syntax ==> str.split(sep=None, maxsplit=-1)


'This is a string'.split()  #  ['This','is','a','string']

# with a sep

'Manchester, London , Liverpool'.split(', ')   ['Manchester','London','Liverpool']
        

'this is a sentence'.split('e')

['this is a s', 'nt', 'nc', '']        

To view or add a comment, sign in

More articles by Bin Amin

  • Python data types.

    A mentor once told me, it is very important to perfectly understand the core concepts of a programming language than…

Others also viewed

Explore content categories