【py code everyday line 1】2023-02-23

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值