-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_basics.py
More file actions
56 lines (43 loc) · 1.55 KB
/
Copy pathstring_basics.py
File metadata and controls
56 lines (43 loc) · 1.55 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
# Blog: https://bumbii.com/chuoi-ky-tu-string-trong-python/
# 1. Declare strings
# Declare strings using single quotes or double quotes
single_quotes_string = 'Hello, Bumbii - single quotes!'
print(single_quotes_string)
double_quotes_string = "Hello, Bumbii! - double quotes"
print(double_quotes_string)
# Strings contain single quotes or double quotes
string_has_single_quotes = "Hello, 'Bumbii'"
print(string_has_single_quotes)
string_has_double_quotes = 'Hello, "Bumbii"'
print(string_has_double_quotes)
# Use back slash to escape the quotes
string_using_back_slash_single_quotes = 'Hello, \'Bumbii\''
print(string_using_back_slash_single_quotes)
string_using_back_slash_double_quotes = "Hello, \'Bumbii\'"
print(string_using_back_slash_double_quotes)
# Use raw string
a_raw_string_single_quotes = r'D:\data\python'
print(a_raw_string_single_quotes) # D:\data\python
a_raw_string_double_quotes = r"D:\data\python"
print(a_raw_string_double_quotes) # D:\data\python
multiple_lines_string_single_quotes = '''
Why do programmers prefer dark mode?
Because light attracts bugs!
Thanks!
'''
print(multiple_lines_string_single_quotes)
multiple_lines_string_double_quotes = """
Why do programmers prefer dark mode?
Because light attracts bugs!
Thanks!
"""
print(multiple_lines_string_single_quotes)
# 2. Use variables in string
year_of_birth = 1988
age = 2024 - year_of_birth
message = f"Your year of birth is {year_of_birth}. You are {age} years old"
print(message)
# 4. Get the length of a string
greeting = "Hello Bumbii!"
length = len(greeting)
print(length) # 13