-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_func.py
More file actions
61 lines (43 loc) · 1.19 KB
/
string_func.py
File metadata and controls
61 lines (43 loc) · 1.19 KB
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
print("Python string functions \n----------------------------\n")
st="string"
# can iterate string.like list , single line for loop with reversed
# for i in reversed(st): print(i)
# print string riverse
print(st[::-1])
# print string with index
print(st[2:4])
# convert string to list and voice versa
lst=list(st)
print(lst)
st="".join(lst)
print(st)
# replace a character in string
st1=st.replace('r','#')
print(st1)
# delete string
# del st1
# Formatting of Strings
# Default order
String1 = "{} {} {}".format('Geeks', 'For', 'Life')
print("Print String in default order: ")
print(String1)
# Positional Formatting
String1 = "{1} {0} {2}".format('Geeks', 'For', 'Life')
print("\nPrint String in Positional order: ")
print(String1)
# Keyword Formatting
String1 = "{l} {f} {g}".format(g='Geeks', f='For', l='Life')
print("\nPrint String in order of Keywords: ")
print(String1)
# Extract sub string between 2 strings
str="my name will be some things better"
print(str)
s1=str.index("will")
s2=str.index("ing")
s3=""
print(s1,s2)
for i in range(s1+len('will'),s2):
s3=s3+str[i]
print(s3)
# or another method using str[4:5] method
print(str[str.index("will")+ len('will'):str.index("ing")])