-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_strings_tutorial.py
More file actions
90 lines (73 loc) · 2.8 KB
/
02_strings_tutorial.py
File metadata and controls
90 lines (73 loc) · 2.8 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
# 1. Creating strings
# Strings can be created with single or double quotes
single = 'Hello'
double = "World"
multiline = """This is
a multiline
string"""
print(single, double)
print(multiline)
# 2. String concatenation (joining)
first_name = "Pavel"
last_name = "Ivanov"
full_name = first_name + " " + last_name
print(f"Full name: {full_name}")
# 3. String formatting (f-strings - the most modern way)
age = 25
message = f"My name is {first_name}, I am {age} years old"
print(message)
# With expressions inside
print(f"In 5 years I will be {age + 5} years old")
# 4. String indexing (accessing characters)
text = "Python"
print(f"First character: {text[0]}") # P
print(f"Last character: {text[-1]}") # n
print(f"Second from end: {text[-2]}") # o
# 5. String slicing
text = "Hello, World!"
print(f"First 5 characters: {text[0:5]}") # Hello
print(f"From 7th to end: {text[7:]}") # World!
print(f"Last 6 characters: {text[-6:]}") # World!
print(f"Every second character: {text[::2]}") # Hlo ol!
print(f"String reversed: {text[::-1]}") # !dlroW ,olleH
# 6. String methods (main ones)
sample = " python programming "
# Removing whitespace
print(f"strip(): '{sample.strip()}'") # removes spaces from both sides
print(f"lstrip(): '{sample.lstrip()}'") # from left
print(f"rstrip(): '{sample.rstrip()}'") # from right
# Changing case
text = "Python"
print(f"upper(): {text.upper()}") # PYTHON
print(f"lower(): {text.lower()}") # python
print(f"capitalize(): {text.capitalize()}") # Python
print(f"title(): 'hello world'.title()") # Hello World
# Search and replace
sentence = "I learn Python. Python is a great language!"
print(f"count('Python'): {sentence.count('Python')}") # 2
print(f"find('Python'): {sentence.find('Python')}") # 7 (index of first occurrence)
print(f"replace(): {sentence.replace('Python', 'JS')}")
# Checks
print(f"'Python'.isalpha(): {'Python'.isalpha()}") # True (only letters)
print(f"'123'.isdigit(): {'123'.isdigit()}") # True (only digits)
print(f"'Py123'.isalnum(): {'Py123'.isalnum()}") # True (letters and digits)
# 7. Splitting and joining strings
csv = "apple,banana,orange"
fruits = csv.split(',')
print(f"split(','): {fruits}")
joined = " - ".join(fruits)
print(f"join(): {joined}")
# 8. Checking substring presence
text = "Python for beginners"
print(f"'Python' in text: {'Python' in text}") # True
print(f"'Java' not in text: {'Java' not in text}") # True
# 9. String length
text = "Hello"
print(f"len('{text}'): {len(text)}") # 5
# 10. Escaping special characters
# \n - newline, \t - tab, \\ - backslash, \' - single quote
escaped = "String with \nnewline and \ttab"
print(escaped)
# Raw-strings (don't interpret escape sequences)
path = r"C:\users\name\documents"
print(path)