字符串是不可变数据类型,不可以进行更改,更改后就是一个新的对象了,因为物理地址发生了变化
一、字符串的创建
字符串是python总常用的数据类型。可以用引号来创建字符串,引号可以是单引号、双引号、三引号,三引号可以写多行字符串
# 单引号创建字符串
str = 'hello world'
# 双引号创建字符串
str1 = "hello world"
# 三引号创建字符串
str2 = """hello world
hello world
hello world
hello world
"""
空字符以及空白字符的表示
# 空字符
a1 = ''
# 空白字符
a2 = ' '
print(bool(a1)) # 打印结果为:False
print(bool(a2)) # 打印结果为:True
二、字符串的运算符
1、+:字符串拼接
# +,字符串可以用 + 号,表示字符串拼接起来
family_name = 'zhang'
last_name = 'xiaomei'
print(family_name + ' ' + last_name) # 打印结果为:zhang xiaomei
print('666' + '888') # 打印结果为:666888
2、* :字符串 * 数字,表示重复多少遍,不支持字符串 * 字符串,会报错:TypeError
print('hello' * 5) # 打印结果为:hellohellohellohellohello
3、in 和 not in:字符串的成员运算,返回的结果为bool类型
# 成员运算符:in not in
str = 'hello world'
print('h' in str) # 打印结果为:True
print