-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.py
76 lines (55 loc) · 1.92 KB
/
strings.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
# Strings
"""
Strings are immutable. You can not modify them in place.
Use str.join() to Join Strings
1. Concatenation with + results in temporaries
2. str.join() inserts a separator between a collection of strings
3. Call join() on the separator string
"""
len("OrHasson")
colors = ';'.join(['#1234', '#1234', '#1234', '#1234', '#1234'])
print(colors)
colors.split(';')
print(colors)
print(''.join(['Or', 'Hasson']))
print("unforgetable".partition('forget'))
departure, separator, arrival = "Tel-Aviv:Tokyo".partition(':')
print(departure)
print(separator)
print(arrival)
origin, _, destination = "Tel-Aviv:New-York".partition(':')
print(origin)
print(destination)
# String formatting
print("{0} north {1} east".format(10, 30))
print("The formatting order is like the string {} and the string {}".format("this1", "this2"))
print("Galactic position x={pos[0]}, y={pos[0]},z={pos[0]}".format(pos=(1, 2, 3)))
import math
print("Math constants: pi={m.pi}, e={m.e}".format(m=math))
print("Math constants: pi={m.pi:.3f}, e={m.e:.3f}".format(m=math))
import datetime
print(f"The current time is {datetime.datetime.now().isoformat()}.")
# Range
"""
Sequence representing an arithmetic progression of integers
"""
print(list(range(5, 10)))
print(list(range(0, 10, 2))) # step of 2
"""
enumerate:
Constructs an iterable of (index, value) tuples around another iterable object
"""
t = [10, 12, 15, 312321, 123123312]
for index, value in enumerate(t):
print(f"i={index}, v = {value}")
str_to_arr = "HelloWorld"
print(list(str_to_arr))
"""
Summary:
-> Use str.join() for efficient string concatenation.
-> Use str.partition() for certain simple string parsing operations.
-> str.format() is a powerful string interpolation technique.
-> f-strings are a kind of string literal for performing interpolation.
-> range() can be called with one,two, or three arguments: start, stop and step.
-> enumerate is ofter better than range for making loop counters.
"""