Professional Documents
Culture Documents
Tut 2 DV
Tut 2 DV
Tut 2 DV
ipynb - Colaboratory
1 #capitalize operation Converts the first character to upper case
2 a = "hello"
3 print(a.capitalize())
Hello
1 #casefold operation Converts string into lower case
2 b = "HELLO"
3 print(b.casefold())
hello
1 #center operation Returns a centered string
2 c = "Hello World"
3 print(c.center(50,"W"))
4 print(c.center(50, ))
WWWWWWWWWWWWWWWWWWWHello WorldWWWWWWWWWWWWWWWWWWWW
Hello World
1 #count operation Returns the number of times a specified value occurs in a string
2 d = "Hello"
3 print(d.count("l"))
1 #encode operation Returns an encoded version of the string
2 e = "Hello"
3 print(e.encode())
4 e = "Hello World"
5 print(e.encode())
b'Hello'
b'Hello World'
1 #endswith operation Returns true if the string ends with the specified value
2 f = "Hello"
3 print(f.endswith("o"))
True
1 #expandtabs Sets the tab size of the string
2 g = "He\tllo\tWor\tld"
3 print(g.expandtabs(16))
He llo Wor ld
1 #find operation Searches the string for a specified value and returns the position of where it was found
2 h = "Hello"
3 print(h.find("l"))
1 #format operation Formats specified values in a string
2 i = "Hello {world}"
3 print(i.format(world = "World"))
Hello World
1 #format_map Formats specified values in a string
2 # input stored in variable a.
3 a = {'x':'John', 'y':'Wick'}
4
5 # Use of format_map() function
6 print("{x}'s last name is {y}".format_map(a))
1 #index operation Searches the string for a specified value and returns the position of where it was found
2 j = "Hello"
3 print(j.index("l"))
https://colab.research.google.com/drive/1cFocVz06UiIob_wW2UyJBoREolradael#scrollTo=Ao8nEzaMTdjs&printMode=true 1/5
8/4/23, 9:38 PM Tut.ipynb - Colaboratory
1 #isalnum operation Returns True if all characters in the string are alphanumeric
2 k = "H3llo"
3 print(k.isalnum())
True
1 #isalpha operation Returns True if all characters in the string are in the alphabet
2 print("H3llo".isalpha())
False
1 #isascii operation Returns True if all characters in the string are ascii characters
2 print("Hello".isascii())
True
1 #isdecimal operation Returns True if all characters in the string are decimals
2 print("101".isdecimal())
3 print("101Hello".isdecimal())
True
False
1 #isdigit operation Returns True if all characters in the string are digits
2 print("101101".isdigit())
3 print("101.101".isdigit())
True
False
1 #isidentifier operation Returns True if the string is an identifier
2 a = "MyFolder"
3 b = "Demo002"
4 c = "2bring"
5 d = "my demo"
6
7 print(a.isidentifier())
8 print(b.isidentifier())
9 print(c.isidentifier())
10 print(d.isidentifier())
True
True
False
False
1 #islower operation Returns True if all characters in the string are lower case
2 print("hello".islower())
True
1 #isnumeric operation Returns True if all characters in the string are numeric
2 print("H3llo".isnumeric())
False
1 #isprintable operation Returns True if all characters in the string are printable
2 txt = "Hello!\nAre you #1?"
3
4 x = txt.isprintable()
5
6 print(x)
7 txt = "Hello! Are you #1?"
8
9 x = txt.isprintable()
10 print(x)
False
True
1 #isspace operation Returns True if all characters in the string are whitespaces
2 print("".isspace())
3 print(" ".isspace())
False
True
https://colab.research.google.com/drive/1cFocVz06UiIob_wW2UyJBoREolradael#scrollTo=Ao8nEzaMTdjs&printMode=true 2/5
8/4/23, 9:38 PM Tut.ipynb - Colaboratory
1 #istitle operation Returns True if the string follows the rules of a title
2 print("Hello".istitle())
3 print("hello".istitle())
True
False
1 #isupper operation Returns True if all characters in the string are upper case
2 print("HELLO".isupper())
True
1 #join operation Converts the elements of an iterable into a string
2 str = '-'.join('hello')
3 print(str)
h-e-l-l-o
1 #ljust operation Returns a left justified version of the string
2 print(" Hello".ljust(20))
3 txt = "banana"
4
5 x = txt.ljust(20)
6
7 print(x, "is my favorite fruit.")
Hello
banana is my favorite fruit.
1 #lower operation Converts a string into lower case
2 print("HELLO".lower())
hello
1 #lstrip operation Returns a left trim version of the string
2 print(" HEllo".lstrip())
HEllo
1 #maketrans operation Returns a translation table to be used in translations
2 # Python3 code to demonstrate
3 # translations using
4 # maketrans() and translate()
5
6 # specify to translate chars
7 str1 = "wy"
8
9 # specify to replace with
10 str2 = "gf"
11
12 # delete chars
13 str3 = "u"
14
15 # target string
16 trg = "weeksyourweeks"
17
18 # using maketrans() to
19 # construct translate
20 # table
21 table = trg.maketrans(str1, str2, str3)
22
23 # Printing original string
24 print ("The string before translating is : ", end ="")
25 print (trg)
26
27 # using translate() to make translations.
28 print ("The string after translating is : ", end ="")
29 print (trg.translate(table))
30
1 #partition operation Returns a tuple where the string is parted into three parts
2 txt = "I could eat bananas all day"
3
4 x = txt.partition("bananas")
https://colab.research.google.com/drive/1cFocVz06UiIob_wW2UyJBoREolradael#scrollTo=Ao8nEzaMTdjs&printMode=true 3/5
8/4/23, 9:38 PM Tut.ipynb - Colaboratory
5
6 print(x)
1 #replace operation Returns a string where a specified value is replaced with a specified value
2 print("Hello")
3 print("Hello".replace("Hello", "Konichiwa"))
Hello
Konichiwa
1 #rfind operation Searches the string for a specified value and returns the last position of where it was found
2 print("Hello".rfind("l"))
1 #rindex operation Searches the string for a specified value and returns the last position of where it was found
2 print("Hello".rindex("l"))
1 #rjust operation Returns a right justified version of the string
2 print(" Hello".ljust(20))
3 txt = "banana"
4
5 x = txt.rjust(20)
6
7 print(x, "is my favorite fruit.")
Hello
banana is my favorite fruit.
1 #rpartition operation Returns a tuple where the string is parted into three parts
2 # String need to split
3 string = "Sita is going to school"
4
5 # Here 'not' is a separator which is not
6 # present in the given string
7 print(string.rpartition('not'))
8
1 #rsplit operation Splits the string at the specified separator, and returns a list
2 word = 'nerds, for, nerds, pawan'
3
4 # maxsplit: 0
5 print(word.rsplit(', ', 0))
6
7 # maxsplit: 4
8 print(word.rsplit(', ', 4))
1 #rstrip operation Returns a right trim version of the string
2 txt = " banana "
3
4 x = txt.rstrip()
5
6 print("of all fruits", x, "is my favorite")
1 #split operation Splits the string at the specified separator, and returns a list
2 txt = "welcome to the jungle"
3
4 x = txt.split()
5
6 print(x)
1 #splitlines operation Splits the string at line breaks and returns a list
2 txt = "Thank you for the music\nWelcome to the jungle"
3
https://colab.research.google.com/drive/1cFocVz06UiIob_wW2UyJBoREolradael#scrollTo=Ao8nEzaMTdjs&printMode=true 4/5
8/4/23, 9:38 PM Tut.ipynb - Colaboratory
4 x = txt.splitlines()
5
6 print(x)
['Thank you for the music', 'Welcome to the jungle']
1 #startswith operation Returns true if the string starts with the specified value
2 txt = "Hello, welcome to my world."
3
4 x = txt.startswith("Hello")
5
6 print(x)
True
1 #strip operation Returns a trimmed version of the string
2 txt = " banana "
3
4 x = txt.strip()
5
6 print("of all fruits", x, "is my favorite")
1 #swapcase operation Swaps cases, lower case becomes upper case and vice versa
2 print("Hello".swapcase())
hELLO
1 #title operation Converts the first character of each word to upper case
2 print("hello".title())
Hello
1 #translate operation Returns a translated string
2 #use a dictionary with ascii codes to replace 83 (S) with 80 (P):
3 mydict = {83: 80}
4 txt = "Hello Sam!"
5 print(txt.translate(mydict))
Hello Pam!
1 #upper operation Converts a string into upper case
2 print("Hello".upper())
HELLO
1 #zfill operation Fills the string with a specified number of 0 values at the beginning
2 txt = "50"
3
4 x = txt.zfill(10)
5
6 print(x)
0000000050
https://colab.research.google.com/drive/1cFocVz06UiIob_wW2UyJBoREolradael#scrollTo=Ao8nEzaMTdjs&printMode=true 5/5