forked from alecktony/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_Python - Strings.py
181 lines (115 loc) · 3.67 KB
/
08_Python - Strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#Creating String with single Quotes
String1 = 'Welcome to Python Programming with NOTTECH'
print('String with the use of Single Quotes : ')
print(String1)
#With Double Quotes
String1 = "I\'m coding"
print('\nString with the use of Double Quotes : ')
print(String1)
#With Triple Quotes
String1 = '''I\'m coding using Python and using my "Laptop"'''
print('\nString with the use of Triple Quotes : ')
print(String1)
#With Multiple Lines
String1 = '''Python
For
Programming'''
print('\nCreating a multiple String')
print(String1)
print('\n')
#################### Accessing characters in Python ##############################
# Python Program to Access characters of String
String1 = 'Python For Programming'
print('Initial String : ')
print(String1)
# Print First Character
print('\nFirst character of String is : ')
print(String1[0])
# Print Last Character
print('\nLast character of String is : ')
print(String1[-1])
# Print 3rd to 12th Character
print('\nSlicing characters from 3-12 : ')
print(String1[3:12])
# Print Characters btn 3rd and 2nd Last Character
print('\nSlicing characters btn ' + '3rd and 2nd last character')
print(String1[3:-2])
#################### Deleting/Updating from a String ##############################
String1 = 'Hello, I\'m a Programmer'
print('\nInitial String : ')
print(String1)
#Updating a character to a string
#String1[2] = 'p'
#print('\nUpdating character at 2nd Index : ')
#print(String1)
#Updating Entire String
String1 = 'Hello, I\'m a Programmer'
print('\nInitial String : ')
print(String1)
#Updating a String
String1 = 'Welcome to the Programming World'
print('Updated String : ')
print(String1)
print('\n')
#Deletion of a character
#String1 = 'Hello'
#print('Initial String : ')
#print(String1)
#Deleting a character of the String
#del String1[2]
#print('\nDeleting character at 2nd Index : ')
#print(String1)
#Deleting Entire String
#String1 = 'Hello, I\'m a Programmer'
#print('Initial String : ')
#print(String1)
#Delete with the use of del
#del String1
#print('\nDeleting entire String : ')
#print(String1)
#Escape Sequencing in Python
#Initial String
String1 = '''I\'m a "Programmer"'''
print('Initial String with the use of Triple Quotes')
print(String1)
#Escaping Single Quote
String1 = 'I\'m a "Programmer"'
print("\nEscaping Single Quote: ")
print(String1)
#Escaping Double Quotes
String1 = "I\'m a \"Programmer\""
print("\nEscaping Double Quotes: ")
print(String1)
#Using Escape Sequences To Print Paths
String1 = "C:\\Python\\Programmer\\"
print("\nEscaping Backslashes: ")
print(String1)
print('\n')
##################Formatting of Strings
#Default Order
String1 = "{} {} {}".format('Python', 'For', 'Programming')
print('Print String in default order : ')
print(String1)
#Positional Formatting
String1 = "{1} {0} {2}".format('Python', 'For', 'Programming')
print("\nPrint String in Positional order: ")
print(String1)
# Keyword Formatting
String1 = '{p} {f} {c}'.format(p = 'Python', f = 'For', c ='Programming')
print("\nPrint String in order of Keywords: ")
print(String1)
# Formatting of Integers
String1 = "{0:b}".format(16)
print("\nBinary representation of 16 is ")
print(String1)
# Formatting of Floats
String1 = "{0:e}".format(165.6458)
print("\nExponent representation of 165.6458 is ")
print(String1)
print('\n')
#Old formatting
Integer1 = 12.3456789
print("Formatting in 3.2f format: ")
print('The value of Integer1 is %3.2f' %Integer1)
print("\nFormatting in 3.4f format: ")
print('The value of Integer1 is %3.4f' %Integer1)