9、Python 字符串操作全解析:创建、提取与拼接

Python 字符串操作全解析:创建、提取与拼接

1. 字符串的创建

在 Python 中,创建字符串最常见的方式是使用引号将文本括起来。单引号、双引号和三引号都能创建字符串对象,且单引号和双引号在创建字符串时可以互换使用,这与 Unix 外壳和 Perl 中的引号使用方式不同。

以下是不同引号创建字符串的示例代码:

string1 = 'This is a string'
string2 = "This is another string"
string3 = '''This is still another string'''
string4 = """And one more string"""
print(type(string1), type(string2), type(string3), type(string4))

输出结果为:

(<type 'str'>, <type 'str'>, <type 'str'>, <type 'str'>)

在 Python 里,如果你需要在字符串中嵌入引号,可以根据引号类型选择合适的引号来避免转义字符的使用。示例如下:

s = "This is a string with 'quotes' in it"
print(s)
s = 'This is a string with \'quotes\' in it'
print(s)
s 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值