-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.py
84 lines (53 loc) · 1.32 KB
/
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
#!/usr/bin/python
str1 = 'Hello World!'
str2 = "I love Python"
print("str1[0]: ", str1[0])
print("str2[7:]: ", str2[7:])
# output:
# str1[0]: H
# str2[7:]: Python
# display multiline
str3 = """
Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the
industry's standard dummy text ever since the 1500s
"""
print("multiline", str3)
# output:
# Lorem Ipsum is simply dummy text of the printing
# and typesetting industry. Lorem Ipsum has been the
# industry's standard dummy text ever since the 1500s
# the len() method return the length of a string
str4 = "Example for length"
print("length", len(str4))
# output:
# length 18
# string in upper case or lower case
str5 = "I Love Python"
print("upper", str5.upper())
print("lower", str5.lower())
# output:
# upper I LOVE PYTHON
# lower i love python
# replace
str6 = "I Love Python"
print(str6.replace("o", "0"))
# output:
# I L0ve Pyth0n
# split
str7 = "I Love Python"
print(str7.split(" "))
# output:
# ['I', 'Love', 'Python']
# format
quantity = 3
price = 10
my_order = "I want {} item for {} dollars"
print(my_order.format(quantity, price))
# output:
# I want 3 item for 10 dollars
# strip, removes any whitespace from the beginning or the end
str8 = " I Love Python "
print(str8.strip())
# output:
# I Love Python