Stripping Whitespace
rstrip() method acts on the variable favorite_language 3, this extra space is
removed. However, it is only removed temporarily.
If you ask for the value of favorite_language again, the string looks the same as when it was entered,
including the extra whitespace
Removing Prefixes(前缀)
When working with strings, another common task is to remove a prefix.
Consider a URL with the common prefix https://.
We want to remove this prefix, so we can focus on just the part of the URL that users need to enter into an address bar. Here’s how to do that
#removeing prefixes
nostarch_url = 'https://nostarch.com'
print(nostarch_url.removeprefix('https://'))
nostarch.com
method
removeprefix()
#removeing prefixes
nostarch_url = 'https://nostarch.com'
#keep the new value with the prefix removed
simple_url = nostarch_url.removeprefix('https://')
print(nostarch_url.removeprefix('https://'))
print(nostarch_url)
print(simple_url)
nostarch.com
https://nostarch.com
nostarch.com
Numbers
Floats
python calls any number with a decimal point a float.
Multiple Assignment
You can assign values to more than one variable using just a single line of
code. This can help shorten your programs and make them easier to read;
you’ll use this technique most often when initializing a set of numbers.
a,b,c = 1,2,3
print(a,b,c)
结果
1 2 3
Constants
A constant is a variable whose value stays the same throughout the life of a program
Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed
MAX_CONNECTIONS = 5000
The Zen of Python
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
removesuffix
2-8. File Extensions:
Python has a removesuffix() method that works exactly
like removeprefix().
Assign the value ‘python_notes.txt’ to a variable called filename.
Then use the removesuffix() method to display the filename without the file extension, like some file browsers do.