1
+ '''
2
+ Created on Aug 7, 2017
3
+
4
+ @author: Aditya
5
+ This program explains the String methods in python
6
+ '''
7
+
8
+ def main ():
9
+ print ('In python Everything is an object.' )
10
+ print ('Even the strings in python are objects.' )
11
+
12
+ print ()
13
+ s = 'This is a string!'
14
+ print (s .upper ())
15
+ print ('This is a string!' .upper ())
16
+
17
+ s = 'this is a lower case string'
18
+ print ('Lower Case string:' ,s )
19
+ print ('Capitalized String:' ,s .capitalize ())
20
+ print ('Upper Case: ' ,s .upper ())
21
+ print ('Lower case:' ,s .lower ())
22
+
23
+ s = 'This Is A Sample'
24
+ print (s )
25
+ print (s .swapcase ())
26
+ print ('Print location of\' sam\' in string:' ,s .find ('Sam' ))
27
+
28
+ print (s .replace ('Sample' ,'String' ))
29
+
30
+ s = ' There is a space at the begining and at the end. '
31
+ print (s )
32
+ print (s .strip ())
33
+ print (s .rstrip ())
34
+
35
+ s = ' String with new line at end\n '
36
+ print (s )
37
+ print (s .strip ())
38
+ print (s .rstrip ('\n ' ))
39
+
40
+ s = 'This is a string'
41
+
42
+ print ('Check if string\' {}\' has only alpha numeric characters in it' .format (s ),'This is false as string has\' spaces\' in it.' )
43
+ print (s .isalnum ())
44
+
45
+ s = 'thisisastring'
46
+ print (s )
47
+ print ('Only_alphanumeric:' ,s .isalnum ())
48
+ print ('check only for letters in string:' ,s .isalpha ())
49
+ print ('check if string has only digits:' ,s .isdigit ())
50
+
51
+ s = '06.3'
52
+ print (s )
53
+ print ('check for digits string:' ,s .isdigit ())
54
+ print ('check decimal:' ,s .isdecimal ())
55
+
56
+ s = '1223456'
57
+ print ('check for digits:' ,s .isdigit ())
58
+ print ('check if printable:' ,s .isprintable ())
59
+
60
+ a ,b = 5 ,6
61
+ print ('A = {}, B = 6' .format (a ,b ))
62
+ print ('B = {1}, A = {0}, B = {1}' .format (a ,b ))
63
+ print ('A({Addo}) and B({Mado})' .format (Addo = 333 ,Mado = 4443 ))
64
+
65
+
66
+ print ()
67
+ print ('Splitting and Joining Strings:' )
68
+ s = 'This is a string.'
69
+ a = s .split ()
70
+ print (a )
71
+ print ('Splitting at\' i\' in string: ' ,s .split ('i' ))
72
+
73
+ for w in a :print (w )
74
+
75
+ s1 = ':' .join (a )
76
+ s2 = ', ' .join (a )
77
+ s3 = ' ' .join (a )
78
+ s4 = '' .join (a )
79
+ print ('{}\n {}\n {}\n {}' .format (s1 ,s2 ,s3 ,s4 ))
80
+
81
+ if __name__ == '__main__' :main ()