-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstring_example.py
107 lines (92 loc) · 2.05 KB
/
string_example.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
def main():
"""
>>> print("ExampleHub")
ExampleHub
>>> my_str = "ExampleHub"
>>> print(my_str)
ExampleHub
>>> my_str = "abc"
>>> my_str[0]
'a'
>>> my_str[1]
'b'
>>> my_str[2]
'c'
>>> my_str[-1]
'c'
>>> my_str = "123456"
>>> for i in range(0, 6):
... assert my_str[i] == str(i + 1)
>>> for char in my_str:
... print(char, end='')
123456
>>> my_str = "abc123456"
>>> my_str[0:3]
'abc'
>>> my_str[:3]
'abc'
>>> my_str[3:9]
'123456'
>>> my_str[3:]
'123456'
>>> my_str[-3:-1]
'45'
>>> my_str[-3:]
'456'
>>> my_str[::-1]
'654321cba'
>>> len("")
0
>>> my_str = "abc123"
>>> len(my_str)
6
>>> my_str = " I love python. "
>>> my_str.strip()
'I love python.'
>>> my_str.strip().lower()
'i love python.'
>>> my_str.strip().upper()
'I LOVE PYTHON.'
>>> my_str.strip().replace("python", "u")
'I love u.'
>>> my_str.split()
['I', 'love', 'python.']
>>> "love" in my_str
True
>>> "LOVE" not in my_str
True
>>> first_name = "Example"
>>> last_name = "Hub"
>>> name = first_name + last_name
>>> name
'ExampleHub'
>>> name = first_name + last_name + "/Python"
>>> name
'ExampleHub/Python'
>>> my_str = "6" * 3
>>> my_str
'666'
>>> name = "Python"
>>> age = 29
>>> intro = "My name is {}. I'am {}.".format(name, age)
>>> intro
"My name is Python. I'am 29."
>>> import math
>>> pi_str = "pi = {:.2f}"
>>> pi_str = pi_str.format(math.pi)
>>> pi_str
'pi = 3.14'
>>> pi_str = f"pi = {math.pi}"
>>> pi_str
'pi = 3.141592653589793'
>>> pi_str = f"pi = {math.pi:.2f}"
>>> pi_str
'pi = 3.14'
>>> my_str = "abc"
>>> del my_str
>>> 'my_str' not in locals()
True
"""
if __name__ == "__main__":
from doctest import testmod
testmod()