Python字符串

字符串就是一系列字符。在Python中,用引号括起来的都是字符串,其中的引号可以是单引号,也可以是双引号,如下所示:

'This is a string.'
"This is also a string."

这种灵活性让你能够在字符串中包含引号和撇号:
‘I told my friend , “Python is my favorite language!”’
“The language ‘Python’ is named after Monty Python,not the snake.”
“One of Python’s strengths is its diverse and supportive community.”

字符串大小写

message = "hello python!"# 原始字符串

print(message)

# 单词首字母大写

print(message.title())

# 全部大写

print(message.upper())


# 全部小写
print(message.lower())

程序运行输出

hello python!
Hello Python!
HELLO PYTHON!
hello python!

合并(拼接)字符串
可以用+号连接

first_name = "Bill"

last_name = "Gates"print(first_name + " " + last_name)

程序运行输出

Bill Gates

删除空白

language = " Python "# 去掉左边空格print(language.lstrip())

# 去掉右边空格print(language.rstrip())

# 去掉左右两边空格print(language.strip())

程序运行输出

Python 
 Python
Python
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡矣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值