-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path22-String.py
140 lines (102 loc) · 3.56 KB
/
22-String.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
'''
author : Jaydatt
string
'''
x = 2
y = 6
print(f'sum of {x} and {y} is {x+y}; product: {x*y}.')
'''
(string with {}....).format(ele1,ele2,.....)
For two decimal places, put :.2f
'''
print('sum of {} and {} is {}; product: {}.'.format( x, y, x+y, x*y))
val = 3.145131
print("Pi : {:.2f}".format(val))
names = ["Alexey", "Catalina", "Misuki", "Pablo"]
print("'{first}!' she yelled. 'Come here, {first}! {f_one}, {f_two}, and {f_three} are here!'".format(f_three = names[3], first = names[1], f_one = names[0], f_two = names[2]))
# error : 'The set is {{{}, {}}}.'.format(a, b)
# %d for signed decimal numbers
# %u for unsigned decimal numbers
# %c for characters
# %f for floating-point real numbers
# %s for strings
string = 'Welcome'
year = 2024
print('%s Python %d'%(string, year))
print('Python %c'%(year))
print('Python %u'%(year))
print('Python %f'%(year))
# Joint strings............
a = "Welcome to "
b = 'World'
print("concate:",a+b)
str_s0 = sorted(a+b)
print("Sorted:",str_s0)
print("len('ABCDE') : ",len('ABCDE'))
''' string slicing..............
A B C D E
0 1 2 3 4 (print 0 to 4 means A-Z)
-5 -4 -3 -2 -1 (print -5 to -1 means A-Z)
'''
print('\nget string from forward positive index..........................')
str_a2z = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print(str_a2z[0:5])
print(str_a2z[2:5])
print(str_a2z[:3])
print(str_a2z[3:])
print ('\nget string from reverse negative index : ')
print(str_a2z[-5:-2])
print(str_a2z[-5:-1])
print(str_a2z[:-3])
print(str_a2z[-3:])
# strings skip Index array............
print('\nstrings skip Index array......................................')
print(str_a2z[0:11:1]) # get string with skip 1
print(str_a2z[0:11:2]) # get string with skip 2
print(str_a2z[0:11:3]) # get string with skip 3
str_s1 = "Welcome To Python"
print('\nstr_s1.upper() : ',str_s1.upper())
print('str_s1.lower() : ',str_s1.lower())
# compare last characters of strings............
str_s2 = 'It is a good one story'
print( "\n'It is a good one story' is ending with 'story' : ",str_s2.endswith("story"))
# find count of word used in strings............
str_s3 = 'It is a good one story. It is very intresting and story is too much old.'
cnt1 = str_s3.count("is")
print( "\n'is' counted ",cnt1)
# make capitalize first character of strings............
str_s4 = 'good story. python.'
print(str_s4)
print("\nstr_s4.capitalize() : ",str_s4.capitalize())
# find character or word in strings (if value >= 0 then its exist, else it is -1 means not exist)
str_s5 = 'It is a good one story.'
print("\nstr_s5.find('good') : ",str_s5.find('good'))
# replace character or word in strings............
str_s6 = 'It is a very cheap.'
str_s6 = str_s6.replace('very','so').replace(' ', '-')
print("\nstr_s6.replace('very','so') : ",str_s6)
# split string into list form
str_s7 = 'It is a very cheap.'
list = str_s7.split()
print("\nstr_s7.split() : ",list)
list = str_s7.split('a')
print("\nstr_s7.split('a') : ",list)
print('type(list)) : ',type(list))
# join list elements in string format
list1 = ['red','blue','green']
str_s8 = ';'
str_s9 = str_s8.join(list1)
print('\nstr_s8.join(list1) : ',str_s9)
# strip() to remove space in start and end of string
str_s10 = " Hello World ! "
print("\nstr_s10.strip() : ",str_s10.strip())
#using partition() method
str_s11 = 'I loved the show'
print("str_s11.partition('the') : ",str_s11.partition('the'))
# comparison of value
a = "banana"
b = "banana"
print("\n(a is b) : ",(a is b))
print("(a == b) : ",(a == b))
print("id(a) : ",id(a))
print("id(b) : ",id(b))