我有一个字符串:
b = 'week'
Run Code Online (Sandbox Code Playgroud)
我想检查最后一个字符是否为"s".如果没有,请附加"s".
这个是否有Pythonic单线程?
您可以使用条件表达式:
b = b + 's' if not b.endswith('s') else b
Run Code Online (Sandbox Code Playgroud)
就个人而言,我仍然坚持使用两条线:
if not b.endswith('s'):
b += 's'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1615 次 |
| 最近记录: |