-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy path#5_Strings_and_String_Methods.py
More file actions
128 lines (102 loc) · 3.25 KB
/
#5_Strings_and_String_Methods.py
File metadata and controls
128 lines (102 loc) · 3.25 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'''
Topic #2: Strings (Chuỗi Ký Tự) - a sequence of characters
'''
# String: ordered, immutable, text presentation
# tạo các chuỗi trong Python bằng cách bao một text trong dấu nháy đơn/kép
# use singe or double quotes
my_string = 'Hello World!'
my_string = "Hello World!"
my_string = "My name's CodeXplore"
# escaping backslash
my_string = 'I\' m a "Cuu Du Hoc Sinh Sing"'
my_string = 'I\' m living in \'Singapore\''
# backslash if you want to continue in the next line
my_string = "Voi mong muon tao nen mot platform \
chia Se nhung Kien Thuc Lap Trinh"
# print(my_string)
'''
Access characters and substrings
'''
my_string = "Hello World"
# get character by referring to index
char = my_string[0] # H
char = my_string[-1] # d
char = my_string[-2] # l
# print(char)
# Cannot be changed
# my_string[0] = 'h' #As String is immutable -> Cannot be changed
# Substrings with slicing
# stringName[startIndex:endIndex]
# start at index 1 and go until index 5 (not include #5)
substring = my_string[1:5]
substring = my_string[:5] # start from begining
substring = my_string[1:] # stop at end of string
substring = my_string[1:5:1] # By default
substring = my_string[::-1] # Reverse the String
# print(substring)
'''
Concatenate two or more strings
'''
# concat strings with +
greeting = "Hello"
name = "Tom"
sentence = greeting + ' ' + name
# print(sentence)
# join elements of a list into a string
my_list = ['How', 'are', 'you', 'doing']
# the given string is the separator, e.g. ' ' between each argument
a = ' '.join(my_list)
'''
Iterating
'''
# Iterating over a string by using a for in loop
my_string = 'Hello'
# for char in my_string:
# print(char)
'''
Check if a character or substring exists
'''
# if "e" in "Hello":
# print("yes")
# if "llo" in "Hello":
# print("yes")
'''
Basic & Useful Function (Hàm Cơ Bản và Hữu Ích)
'''
# 'I am alone' --> Strips all whitespace characters from both ends.
print(' I am alone '.strip())
# 'On an islan' --> # Strips all passed characters from both ends.
'On an island'.strip('d')
'but life is good!'.split() # ['but', 'life', 'is', 'good!']
'but, quite Boring'.split(',') # ['but', 'quite Boring']
# 'Help you' --> Replaces first with second param
'Help me'.replace('me', 'you')
'Need to make fire'.startswith('Need') # True
'and cook rice'.endswith('rice') # True
# returns the starting index position of the first occurence
'bye bye'.index('e') # 2
'oh hi there'.find('i') # 4
'still there?'.upper() # STILL THERE?
'HELLO?!'.lower() # hello?!
'how are you?'.title() # How Are You?
'true'.title() # True => convert to Boolean eval(True)
'ok, I am done.'.capitalize() # 'Ok, I am done.'
'oh hi there'.count('e') # 2
'''
String Formatting
'''
# %, .format(), f-Strings
# %
name = "CodeXplore"
my_string = "Variable is %s" % name
pi = 3.14159
s = "Variable pi"
my_string = "Variable is %.2f from %s" % (pi, s)
# .format()
age = 24
my_string = "Variable is {:.3f} and {}".format(pi, age)
# f-Strings
my_string = f"Variable is {pi:.2f} and {age}"
# f-Strings are evaluated at runtime, which allows expressions
my_string = f"The value is {2*60}"
print(my_string)