-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-string.py
More file actions
81 lines (59 loc) · 1.33 KB
/
python-string.py
File metadata and controls
81 lines (59 loc) · 1.33 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
str = "Text"
print("->", str.center(100), "<-")
str = "11223344556622222"
# returns the number of occurrences
print(str.count("2"))
# index of the first occurrence
print("index:", str.find("2"))
# string to number
s = '10'
print(int(s))
# char array
str1 = "chararray"
print(list(str1))
# slicing
# seq [start:end:step]
str = "Test string"
print(str[-1])
print(str[-2])
print(str[-2:])
print(str[0::2])
print(str[0:-2])
name = "Text"
print(name.rjust(50,"-"))
print(name.ljust(50,"-"))
# format
print("{0} {1}".format("1", "2"))
print("{} {}".format("1", "2"))
text_with_lines = "text1\ntext2\ntext3"
print(text_with_lines.splitlines())
#split
print(text_with_lines.split("\n"))
#strip
print("text1|text2".strip("text1"))
#swapcase
print("text".swapcase())
print("text".capitalize())
print("text".upper())
print("TEXT".lower())
# index or find
# index throw exception
print("text1".find("t"))
print("text1".find("a"))
print("text1".rfind("t"))
print("text1".rfind("1"))
# count(t,start,end)
print("text1".count("t", 0, -1))
# replace (t,u,n) - replace n times
print("text1".replace("t", "r", 1))
print("text1".replace("t", "r", 2))
# chr
print(chr(76))
print(ord('K'))
# is alphanumeric
print("abc123".isalnum())
print("abc123!".isalnum())
print("123".isdigit())
print("123".isnumeric())
print("8".isdigit())
print(max("abcd12912zopwe"))