python字符串的一些笔记 part1

in Learn to Codelast month

修改首字母大小写

使用.title()
示例

注意.title()后面的()留空即可,并且需要是英文半角name = 'ada lovelace' print(name.title())

让全字段都大写或者小写

大写

使用.upper()

小写

使用.lowe()
规则同上

在字符串中使用变量

先看这一段代码

看看输出的结果

可知f'{}'即使调用前面的变量
其实是f'任意字符串{}'要调用的字段名必须用{}包裹起来
可以看看这一段

first_name = 'ada' last_name = 'lovelace' full_name = f'{first_name}{last_name}' print(full_name)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

Sort:  

Congratulations @zmx! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You distributed more than 125000 upvotes.
Your next target is to reach 130000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP