Skip to the content.
Home | Blog | Book Reading |About | All Courses

String Methods


List of String Methods mentioned in this post

| | | | | | | - | - | - | - | - | | 1. [capitalize()](#capitalize) | 2. [casefold()](#casefold) | 3. [center()](#center) | 4. [count()](#count) | 5. [encode()](#encode) | | 6. [endswith()](#endswith)| 7. [expandtabs()](#expandtabs) | 8. [find()](#find) | 9. [format()](#format) | 10. [format_map()](#format_map) | | 11. [index()](#index) | 12. [isalnum()](#isalnum) | 13. [isalpha()](#isalpha) | 14. [isdecimal()](#isdecimal) | 15. [isdigit()](#isdigit) | | 16. [isidentifier()](#isidentifier) | 17. [islower()](#islower)| 18. [isnumeric()](#isnumeric) | 19. [isprintable()](#isprintable) | 20. [isspace()](#isspace) | | 21. [istitle()](#istitle) | 22. [isupper()](#isupper) | 23. [join()](#join) | 24. [ljust()](#ljust) | 25. [lower()](#lower) | | 26. [lstrip()](#lstrip)| 27. [maketrans()](#maketrans)| 28. [partition()](#partition)| 29. [replace()](#replace)| 30. [rfind()](#rfind) | 31. [rindex()](#rindex)| 32. [rjust()](#rjust)| 33. [rpartition()](#rpartition)| 34. [rsplit()](#rsplit)| 35. [rstrip()](#rstrip)| | 36. [splitlines()](#splitlines)| 37. [startswith()](#startswith)| 38. [strip()](#strip) | 39. [swapcase()](#swapcase) | 40. [title()](#title) | | 41. [translate()](#translate) | | 42. [upper()](#upper) | | 43. [zfill()](#zfill) |

capitalize() Converts the first character of the string to a capital (uppercase) letter

>>> string = "welcome to share to learn"
>>> string.capitalize()
'Welcome to share to learn'
>>>

casefold() Implements caseless string matching

>>> string = "welcome to SHARE TO LEARN"
>>> string.casefold()
'welcome to share to learn'

center() Pad the string with the specified character.

>>> str.center(12,"-")
'---Python---'
>>>

count() Returns the number of occurrences of a substring in the string.

>>> string = "welcome to share to learn"
>>>
>>>
>>> string.count("we")
1
>>> string.count("to")
2
>>> string.count("l")
2
>>>

encode() Encodes strings with the specified encoded scheme

>>> string
'welcome to share to learn'
>>> string.encode()
b'welcome to share to learn'
>>>
>>> string.encode("ascii","ignore")
b'welcome to share to learn'
>>>
>>> string.encode("ascii","replace")
b'welcome to share to learn'
>>>

endswith() Returns “True” if a string ends with the given suffix

>>> string
'welcome to share to learn'
>>>
>>> string.endswith("n")
True
>>>
>>>
>>> string.endswith("l")
False
>>> string.endswith("to")
False
>>> string.endswith("learn")
True
>>>

expandtabs() Specifies the amount of space to be substituted with the “\t” symbol in the string

>>> sentence = "10\t20\t30"
>>> sentence
'10\t20\t30'
>>> print(sentence)
10	20	30
>>> print(sentence.expandtabs(tabsize=15))
10             20             30
>>> sentence.expandtabs(tabsize=15)
'10             20             30'

find() Returns the lowest index of the substring if it is found

>>> string = "welcome to share to learn"
>>> string.find("share")
11
>>> string.find("to")
8

format() Formats the string for printing it to console

>>> print("Welcome to {} to {}".format("Share","Learn"))
Welcome to Share to Learn
>>> print("Welcome to {0} to {1}".format("Share","Learn"))
Welcome to Share to Learn
>>> print("Welcome to {1} to {0}".format("Share","Learn"))
Welcome to Learn to Share

format_map() Formats specified values in a string using a dictionary

>>> lunch = {
            "Food": "Pizza", 
            "Drink": "Juice"}
>>> print("Lunch: {Food}, {Drink}".format_map(lunch))
Lunch: Pizza, Juice

>>> print("Lunch: {Food}, {Drink} and {dessert}".format_map(lunch))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'dessert'

>>> class Default(dict):
        def __missing__(self, key):
            '''
                To handle KeyError, when key is not present in dictionary
            '''
            return key
            
>>> print("Lunch: {Food}, {Drink} and {dessert}".format_map(Default(lunch)))
Lunch: Food, Juice and dessert
>>> # Notice that we passed "Default(lunch)" to format_map and not just "lunch"

index() Returns the position of the first occurrence of a substring in a string

>>> string
'welcome to share to learn'
>>> string.index("to")
8
>>>
>>> string.index("t")
8
>>> string.index("o")
4
>>> string.index("learn")
20
>>>

isalnum() Checks whether all the characters in a given string is alphanumeric or not

>>> string
'welcome to share to learn'
>>> string.isalnum()
False
>>> sentence = "HelloPython123"
>>> sentence.isalnum()
True
>>> sentence1 = " $ ***!"
>>> sentence1.isalnum()
False
>>>

isalpha() Returns “True” if all characters in the string are alphabets

>>> sentence
'HelloPython123'
>>> sentence.isalpha()
False
>>> sentence1
'welcome to share to learn'
>>> sentence1.isalpha()
False
>>> sentence = "HelloPython"
>>> sentence.isalpha()
True
>>>

isdecimal() Returns true if all characters in a string are decimal

>>> string
'Welcome to Share to learn'
>>> string.isdecimal()
False
>>> string1 = "500"
>>> string1.isdecimal()
True
>>>

isdigit() Returns “True” if all characters in the string are digits

>>> string1 = "500"
>>> string1.isdigit()
True
>>> string2 = "500.1"
>>> string2.isdigit()
False
>>>

isidentifier() Check whether a string is a valid identifier or not

>>> string1
'500'
>>> string1.isidentifier()
False
>>> string2 = "user1"
>>> string2.isidentifier()
True
>>>

islower() Checks if all characters in the string are lowercase

>>> string
'Welcome to Share to learn'
>>> string.islower()
False
>>> string1 = "welcome to share to learn"
>>> string1.islower()
True
>>>

isnumeric() Returns “True” if all characters in the string are numeric characters

>>> string
'Welcome to Share to learn'
>>> string.isnumeric()
False
>>> string1 = "123456"
>>> string1.isnumeric()
True
>>>

isprintable() Returns “True” if all characters in the string are printable or the string is empty

>>> string
'Welcome to Share to learn'
>>> string.isprintable()
True
>>> string1
'500'
>>> string1.isprintable()
True
>>> string2
'\t'
>>> string2.isprintable()
False
>>>

isspace() Returns “True” if all characters in the string are whitespace characters

>>> string
'Welcome to Share to learn'
>>> string.isspace()
False
>>> sentence = "\t"
>>> sentence.isspace()
True
>>>

istitle() Returns “True” if the string is a title cased string

>>> string
'Welcome to Share to learn'
>>> string.istitle()
False
>>> string1
'500'
>>> string1.istitle()
False
>>> sentence
'\t'
>>> sentence.istitle()
False
>>> sentence1 = "Python"
>>> sentence1.istitle()
True
>>>

isupper() Checks if all characters in the string are uppercase

>>> string
'Welcome to Share to learn'
>>> string.isupper()
False
>>> sentence = "HELLO THERE"
>>> sentence.isupper()
True
>>>

join() Returns a concatenated String

>>> a = "-"
>>> print(a.join("123"))
1-2-3
>>>
>>> a = "##"
>>> print(a.join("HELLO"))
H##E##L##L##O
>>>

ljust() Left aligns the string according to the width specified

>>> sentence = "Python"
>>> a = sentence.ljust(12, "-")
>>> print(a)
Python------
>>>

lower() Converts all uppercase characters in a string into lowercase

>>> sentence
'Welcome to Share to learn'
>>> sentence.lower()
'welcome to share to learn'
>>> sentence1
'HELLO THERE'
>>> sentence1.lower()
'hello there'
>>>

lstrip() Returns the string with leading characters removed

>>> sentence
'   Python  '
>>> sentence.lstrip()
'Python  '
>>>

maketrans() Returns a translation table


partition() Splits the string at the first occurrence of the separator

>>> data
'Welcome-to-sharetolearn'
>>> data.partition("-")
('Welcome', '-', 'to-sharetolearn')
>>>

replace() Replaces all occurrences of a substring with another substring

>>> data
'Hello Python'
>>> data.replace("Hello","Bye")
'Bye Python'
>>>

rfind() Returns the highest index of the substring

>>> sentence
'Hello-Python'
>>> sentence.rfind("P")
6
>>>

rindex() Returns the highest index of the substring inside the string

>>> string
'Welcome to Share to learn'
>>> string.rindex("S")
11
>>> string.rindex("l")
20
>>>

rjust() Right aligns the string according to the width specified

>>> sentence = "Hello Python"
>>> sentence1 = mystr.rjust(20,
"-")
>>> print(sentence1)
--------Hello Python

rpartition() Split the given string into three parts

>>> sentence = "Hello Python"
>>>
print(sentence.rpartition("."))
('', '', 'Hello Python')
>>> print(sentence.rpartition(" "))
('Hello', ' ', 'Python')

rsplit() Split the string from the right by the specified separator

>>> sentence = "Hello Python"
>>> print(sentence.rsplit())
['Hello', 'Python']
>>> sentence = "Hello-Python-Hello"
>>>
print(sentence.rsplit(sep="-", maxsplit=1))
['Hello-Python', 'Hello']

rstrip() Removes trailing characters

>>> sentence
'   Python  '
>>> sentence.rstrip()
'   Python'
>>>
>>> win_msg = "** We Won!!! **"
>>> win_msg
'** We Won!!! **'
>>> sentence.rstrip('*')
' We Won!!! '
>>>

splitlines() Split the lines at line boundaries

>>> sentence = '''
    Welcome to Share to Learn
    Happy Learning
'''
>>> sentence
'\nWelcome to Share to Learn\nHappy Learning\n'
>>> sentence.splitlines()
['', 'Welcome to Share to Learn', 'Happy Learning']
>>>

startswith() Returns “True” if a string starts with the given prefix

>>> sentence = "Welcome to Share to Learn"
>>> sentence.startswith("W")
True
>>> sentence.startswith("w")
False
>>> sentence.startswith("s")
False
>>>

strip() Returns the string with both leading and trailing characters

>>> sentence = "  Welcome to Share to Learn  "
>>> sentence.strip()
'Welcome to Share to Learn'
>>>

swapcase() Converts all uppercase characters to lowercase and vice versa

>>> sentence = "Welcome to Share to Learn"
>>> sentence.swapcase()
'wELCOME TO sHARE TO lEARN'

title() Convert string to title case

>>> sentence = 'WELCOME TO SHARE TO LEARN'
>>> sentence.title()
'Welcome To Share To Learn'

>>> sentence = 'welcome to share to learn'
>>> sentence.title()
'Welcome To Share To Learn'

>>> sentence = 'wELCOME TO sHARE TO lEARN'
>>> sentence.title()
'Welcome To Share To Learn'

translate() Modify string according to given translation mappings

>>> frm = "helloPython"
>>> to = "40250666333"
>>> trans_table =
str.maketrans(frm, to)
>>> secret_code = "Secret Code".translate(trans_table)
>>> print(secret_code)
S0cr06 C3d0

upper() Converts all lowercase characters in a string into uppercase

>>> sentence = "welcome to python"
>>> sentence.upper()
'WELCOME TO PYTHON'

zfill() Returns a copy of the string with ‘0’ characters padded to the left side of the string

>>> sentence = "1234"
>>> sentence.zfill(5)
'01234'
>>> sentence.zfill(9)
'000001234'
>>>