Python
/
Strings
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Strings Raw Strings
Raw strings prints any backslash In a f-string braces are used insteed of %s r'That\'s my cat' f'My name is {name}'Embed
Raw strings
2 p131 Raw strings prints any backslash in the string.
"""Raw strings:
A raw string ignores all escape characters"""
print('That\'s my cat')
# That's my cat
print(r'[a-z\'A-Z]')
# [a-z\'A-Z]
Interpolation
2 p134 You don't have to use str() to convert values to string.
"""String interpolation:
The %s operator inside the string is a marker to be replaced.
"""
name = 'John'
age = 40
message = 'My name is %s and I\'m %s old'
print(message % (name, age))
# My name is John and I'm 40 old
print("I am %s and have %s dogs " % ("John", 2))
# I am John and have 2 dogs
F-String
2 p134 In a f-string braces are used insteed of %s
"""f-string:
Is similar to string interpolation, except that ...
braces are used insteed of %s
The same result can be obtain using format() method.
"""
name = 'John'
age = 40
message = f'My name is {name} and I\'m {age} old'
print(message)
# My name is John and I'm 40 old
print('My name is {} and I am {} old'.format(name, age))
# My name is John and I am 40 old
print('My name is {0}'.format(name, age))
# My name is John
Strip
2 p142 Strip off whitespace characters (\s, \t, \n)
"""Removing white spaces:
Use strip(), rstrip() and lstrip() ...
to strip off whitespace characters (\s, \t, \n)
"""
HELLO = ' Hello World '
HELLO_STRIP = HELLO.strip()
HELLO_LSTRIP = HELLO.lstrip()
HELLO_RSTRIP = HELLO.rstrip()
assert f'x{HELLO_STRIP}x' == 'xHello Worldx'
assert f'x{HELLO_LSTRIP}x' == 'xHello World x'
assert f'x{HELLO_RSTRIP}x' == 'x Hello Worldx'
➥ Questions