-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringStuff.py
More file actions
100 lines (76 loc) · 1.73 KB
/
stringStuff.py
File metadata and controls
100 lines (76 loc) · 1.73 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# specific character in string (string as array)
a = "Hello, World!"
print(a[1])
# loop string
for x in "banana":
print(x)
# length of string
print(len(a))
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
txt = "The best things in life are free!"
if "expensive" not in txt:
print("Yes, 'expensive' is NOT present.")
# slicing
b = "Hello, World!"
print(b[2:5])
# slicing from start
b = "Hello, World!"
print(b[:5])
# slicing to end
b = "Hello, World!"
print(b[2:])
# negative indexing
b = "Hello, World!"
print(b[-5:-2])
# uppercase
a = "Hello, World!"
print(a.upper())
# lowercase
a = "Hello, World!"
print(a.lower())
# trim
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
# replace
a = "Hello, World!"
print(a.replace("H", "J"))
# split
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
for x in a.split(","):
print(x)
# concatenate
a = "Hello"
b = "World"
c = a + b
print(c)
a = "Hello"
b = "World"
c = a + " " + b
print(c)
# string format
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age, name))
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname="Ford", model="Mustang"))
txt = "For only {price:.2f} dollars!"
print(txt.format(price=49))
# escape
txt = "We are the so-called \"Vikings\" from the north."