4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

python字符串的一些笔记 part1

修改首字母大小写

使用.title()
示例
name = 'ada lovelace'
print(name.title())

注意.title()后面的()留空即可,并且需要是英文半角

让全字段都大写或者小写

大写

使用.upper()

小写

使用.lowe()
规则同上

在字符串中使用变量

先看这一段代码
first_name = 'ada'
last_name = 'lovelace'
full_name = f'{first_name}{last_name}'
print(full_name)

看看输出的结果

可知f'{}'即使调用前面的变量
其实是f'任意字符串{}'要调用的字段名必须用{}包裹起来
可以看看这一段
first_name = 'ada'
last_name = 'lovelace'
full_name = f'first_name,last_name{first_name}{last_name}'
print(full_name)

使用制表符或换行符来添加空白

制表符
\t
换行符
\n

删除空白

末尾

.rstrip

开头

.lstrip

前后

.strip

15 upvotes $0.11

Replies (1)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: