Day11-String Methods # 3
Part # 3

| METHODS | METHODS | METHODS |
| isascii, isdecimal, isdigital | replace | rjust, rjust |
| isidentifier | swapcase | format, format_map |
| isprintable, isspace, istitle | translate, maketrans | expandtabs, encode |
| title | partition, rpartition | zfill |
isascii ( ):
It returns True if all the characters are ascii characters (a-z) or (A-Z) or (0-9).
isdecimal ():
The isdecimal() method - Returns True if all the characters are decimals (0-9). This method can also be used on unicode objects.
isdigit ( ):
The isdigit() method returns True if all the characters are digits, otherwise False. Exponents, like ², are also considered to be a digit.
Example:
#isacscii( )
txt = "Company123"
x = txt.isascii()
print(x) #output: True
#isdecimal
txt = "1234"
y = txt.isdecimal()
print(y) #output: True
#isdecimal
a = "\u0030" #unicode for 0
print(a.isdecimal()) #output: True
#isdigit
txt = "50800"
z = txt.isdigit()
print(z) #output: True
isidentifier ( ):
It returns True if the string is a valid identifier, otherwise False.
A string is considered a valid identifier if it only contains alphanumeric letters (a-z) and (0-9), or underscores (_). A valid identifier cannot start with a number, or contain any spaces.
Example:
a = "MyFolder"
b = "Demo002"
c = "2bring"
d = "my demo"
print(a.isidentifier()) #output: True
print(b.isidentifier()) #output: True
print(c.isidentifier()) #output: False
print(d.isidentifier()) #output: False
isprintable ( ) :
It returns True if all the values within the given string are printable, if not, then return False.
Example:
str1 = "We wish you a Merry Christmas"
print(str1.isprintable()) #output: True
str2 = "We wish you a Merry Christmas\n"
print(str2.isprintable()) #output: False
# here\n is non-printable character that's why it returns false
isspace ( ) :
It returns True only and only if the string contains white spaces, else returns False.
Example:
str1 = " " #using Spacebar
print(str1.isspace()) #output: True
str2 = " " #using Tab
print(str2.isspace()) #output: True
istitle ( ) :
It returns True only if the first letter of each word of the string is capitalized, else it returns False.
Example:
str1 = "World Health Organization"
print(str1.istitle()) #output: True
str2 = "To kill a Mocking bird"
print(str2.istitle()) #output: False
title ( ) :
This method capitalizes each letter of the word within the string.
Example:
str1 = "He's name is Dan. Dan is an honest man."
#all word first character become capital
print(str1.title()) #output: He's Name Is Dan. Dan Is An Honest Man.
replace ( ) :
This method replaces all occurences of a string with another string.
Example:
a = "Hafsa learns coding"
print(a.replace("Hafsa", "John")) #here John replace Hafsa
#output: John learns coding
txt = "one one was a race horse, two two was one too."
x = txt.replace("one", "three", 2) #here one replace by three at index[2]
print(x) #output: three three was a race horse, two two was one too."
swapcase ( ) :
This method changes the character casing of the string. Upper case are converted to lower case and lower case to upper case.
Example:
str1 = "Python is a Interpreted Language"
#convert uppercase to lowercae & lowercase to uppercase.
print(str1.swapcase()) #output: pYTHON IS A iNTERPRETED lANGUAGE
translate():
The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table.
Use the maketrans() method to create a mapping table.
If a character is not specified in the dictionary/table, the character will not be replaced.
If you use a dictionary, you must use ascii codes instead of characters.
Example:
#use a dictionary with ascii codes to replace 83 (S) with 80 (P):
mydict = {83: 80}
txt = "Hello Sam!"
print(txt.translate(mydict)) #output: Hello Pam!
txt = "Hi Sam!"
x = "mSa"
y = "eJo"
mytable = str.maketrans(x, y)
print(txt.translate(mytable)) #output: Hi Joe!
maketrans ( ):
The maketrans() method - Returns a mapping table that can be used with the translate() method to replace specified characters.
Example:
txt = "Hello Sam!"
mytable = str.maketrans("S", "P")
print(txt.translate(mytable)) #output: Hello Pam!
partition ( ):
The partition() method - Searches for a specified string, and splits the string into a tuple containing three elements.
The first element contains the part before the specified string.
The second element contains the specified string.
The third element contains the part after the string.
Example:
txt = "I could eat bananas all day"
x = txt.partition("bananas")
print(x) #output: ('I could eat ', 'bananas', ' all day')
rpartition ( ):
The rpartition() method searches for the last occurrence of a specified string, and splits the string into a tuple containing three elements.
The first element contains the part before the specified string.
The second element contains the specified string.
The third element contains the part after the string.
Example:
txt = "I could eat bananas all day, bananas are my favorite fruit"
x = txt.rpartition("bananas")
print(x)
#output: ('I could eat bananas all day, ', 'bananas', ' are my favorite fruit')
ljust ( ):
The ljust() method - will left align the string, using a specified character (space is default) as the fill character.
rjust ( ):
The rjust() method will right align the string, using a specified character (space is default) as the fill character.
Example:
#ljust
txt = "banana"
x = txt.ljust(20)
print(x, "is my favorite fruit.")
#output: banana is my favorite fruit.
txt = "banana"
y = txt.ljust(20, "O")
print(y) #output: bananaOOOOOOOOOOOOOO
Note: In the result, there are actually 14 whitespaces to the right of the word banana.
#rjust
txt = "banana"
x = txt.rjust(20)
print(x, "is my favorite fruit.")
#output: banana is my favorite fruit.
txt = "banana"
x = txt.rjust(20, "O")
print(x) #output: OOOOOOOOOOOOOObanana
format ( ):
The format() method - Formats given values in a string
Example:
#named indexes:
txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
#output: My name is John, I'm 36
#numbered indexes:
txt2 = "My name is {0}, I'm {1}".format("John",36)
#output: My name is John, I'm 36
#empty placeholders:
txt3 = "My name is {}, I'm {}".format("John",36)
#output: My name is John, I'm 36
format_map ( ):
The format_map() method - Formats specified given values in a string.
Example:
point = {'x':4,'y':-5}
print('{x} {y}'.format_map(point)) #output: 4 -5
point = {'x':4,'y':-5, 'z': 0}
print('{x} {y} {z}'.format_map(point)) #output: 4 -5 0
expandtabs ( ):
The expandtabs() method - Sets the tab size to the specified number of whitespaces.
Example:
txt = "H\te\tl\tl\to"
x = txt.expandtabs(2)
print(x) #output: H e l l o
encode ( ):
The encode() method - Encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
Example:
txt = "My name is Ståle"
x = txt.encode()
print(x) #output: b'My name is St\xc3\xe5le'
zfill ( ):
The zfill() method - Adds zeros (0) at the beginning of the string, until it reaches the specified length.
If the value of the len parameter is less than the length of the string, no filling is done.
Example:
txt = "50"
x = txt.zfill(10)
print(x) #output: 0000000050




