-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringT.py
More file actions
108 lines (87 loc) · 3.46 KB
/
Copy pathstringT.py
File metadata and controls
108 lines (87 loc) · 3.46 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
101
102
103
104
105
106
107
s = "Python"
# ---------- INDEXING ----------
print(s[0]) # P -> first character
print(s[-1]) # n -> last character
# ---------- SLICING ----------
print(s[1:4]) # yth -> start=1, end=4(exclusive)
print(s[:4]) # Pyth -> start defaults to 0
print(s[2:]) # thon -> end defaults to len(s)
print(s[::2]) # Pto -> step=2
print(s[::-1]) # nohtyP -> reverse string
# ---------- IMMUTABILITY ----------
# s[0] = 'J' # ERROR: strings are immutable
print('J' + s[1:]) # Jython -> create new string
# ---------- OPERATORS ----------
print("Py" + "thon") # Python -> concatenation
print("Hi" * 3) # HiHiHi -> repetition
print('P' in s) # True -> membership test
print('X' not in s) # True -> membership test
# ---------- ESCAPE CHARACTERS ----------
print("A\nB") # A
# B -> newline
print("A\tB") # A B -> tab
print("A\\B") # A\B -> backslash
print("A\'B") # A'B -> single quote
print("A\"B") # A"B -> double quote
print(r"A\nB") # A\nB -> raw string
# ---------- FORMATTING ----------
name, age = "John", 25
print(f"{name} {age}") # John 25
print("{} {}".format(name, age)) # John 25
print("%s %d" % (name, age)) # John 25
# ---------- BUILT-IN FUNCTIONS ----------
print(len(s)) # 6 -> length
print(max(s)) # y -> highest ASCII char
print(min(s)) # P -> lowest ASCII char
print(sorted(s)) # ['P','h','n','o','t','y']
# ---------- CASE CONVERSION ----------
print(s.upper()) # PYTHON
print(s.lower()) # python
print(s.capitalize()) # Python
print("hello world".title()) # Hello World
print("PyThOn".swapcase()) # pYtHoN
# ---------- CHARACTER CHECK ----------
#Does every character satisfy this condition?
print("abc".isalpha()) # True
print("123".isdigit()) # True
print("abc123".isalnum()) # True
print(" ".isspace()) # True
print("my_var".isidentifier()) # True
print('ABCD'.isupper()) # True
print('abcd'.islower()) # True
print('5678'.isdigit()) # True
print("Hello World".istitle()) # True
# ---------- ASCII CONVERSION ----------
print(ord('A')) # 65
print(ord('a')) # 97
print(chr(65)) # A
print(chr(97)) # a
# ---------- SEARCHING ----------
print(s.find("th")) # 2 -> returns -1 if absent
print(s.rfind("o")) # 4 -> search from right
print(s.index("th")) # 2 -> error if absent
print(s.count("o")) # 1 -> occurrence count
# ---------- REPLACE ----------
print(s.replace("Py","My")) # Mython
# ---------- STRIP ----------
print(" hello ".strip()) # hello
print(" hello ".lstrip()) # hello_
print(" hello ".rstrip()) # _hello
# _ represents preserved space
# ---------- SPLIT ----------
print("a,b,c".split(",")) # ['a', 'b', 'c']
print("a b c".split()) # ['a', 'b', 'c']
# ---------- JOIN ----------
print("-".join(['a','b','c'])) # a-b-c
# ---------- PREFIX / SUFFIX ----------
print(s.startswith("Py")) # True
print(s.endswith("on")) # True
# ---------- ALIGNMENT ----------
print("Hi".center(10)) # ' Hi '
print("Hi".ljust(10)) # 'Hi '
print("Hi".rjust(10)) # ' Hi'
print("42".zfill(5)) # '00042'
# ---------- COMPARISON ----------
print("abc" == "abc") # True
print("abc" < "abd") # True
print("ABC".lower()=="abc") # True