Python学习笔记--字符串

Python学习笔记

28-字符串

# 创建字符串
str_1 = "hello world"
str_2 = 'hello world'
str_3 = """hello world"""
str_4 = '''hello world'''

# 单引号包括双引号;双引号包括单引号。
m = "i say:'my name is jack'"
n = 'i say:"my name is jack"'

# 转义字符
o = "i say:\"my name is jack\""
p = 'i say:\'my name is jack\''
str_5 = "hello\nworld"  # 换行
str_6 = "hello\tworld"  # 制表符

# 在字符串的前面的加r,可以去除转义字符,原样输出
str_7 = r"hello\nworld"

# 在字符串的前面的加f,可以解析字符串中{}语法
name = "jack"
str_8 = f"hello{name}world"
print(str_8)

# 在字符串的前面的加b,表示该字符串的类型是字节类型
str_9 = b"hello world"
print(type(str_9))  # <class 'bytes'>


29-字符串访问和拼接

# 通过下标访问字符串内容
s = "hello world"
print(s[0])
print(s[-1])

# 遍历字符串
# 第一种方式
for ch in s:
    print(ch, end='')
# 第二种方式,len()
for i in range(len(s)):
    print(s[i], end='-')
# 第三种方式,enumerate()
for k, v in enumerate(s):  # k为下标,v为字符(表示值)
    print(k, v, end='=')

# 字符串拼接使用
# 注意:字符串拼接使用 +
s1 = "hello"
s2 = "world"
print(s1 + s2)

30-字符串切片

# 示例:
string = "Hello, World"
print(string[0:5])  # 输出 "Hello"
print(string[7:])  # 输出 "World!"
print(string[:12])  # 输出 "Hello, World"
print(string[7:12])  # 输出 "World"
print(string[::])  # 输出 "Hello, World!"
print(string[::-1])  # 输出 "dlroW ,olleH"

31-字符串格式化输出

name = "John"
age = 30
money = 1000.55
print('名字:', name, '年龄:', age, '钱:', money)
print("Name: %s, Age: %d, Money: %.2f" % (name, age, money))

# 第二种方法
print(f"Name: {name}, Age: {age}, Money: {money}")
print(f'Name: {name}, Age: {age}, Money: {money:.2f}')

32-字符串长度次数统计

str_1 = "不吃学习的苦,就要吃生活的苦,我认为学习的苦比生活的苦容易一些。"

# 获取字符串的长度:len()
print(len(str_1))  # 32

# 统计字符在指定字符串中出现的次数,count()
print(str_1.count("苦"))  # 4
print(str_1.count("苦", 0, 15))  # 2

33-字符串大小写转换

str_1 = "I miss you Very Much"

print(str_1.upper())  # 转换为大写
print(str_1.lower())  # 转换成小写
print(str_1.swapcase())  # 小转大,大转小;大小写转换
print(str_1.capitalize())  # 转换成首字母大写
print(str_1.title())  # 每个单词首字母大写

34-字符串填充

str_1 = "今天天气很热"
print(str_1)
print(str_1.center(20))  # 使用空格在字符串填充20个长度,原内容居中
print(str_1.center(20, "*"))  # 使用*在字符串填充20个长度,原内容居中

print(str_1.ljust(20))  # 使用空格在字符串填充20个长度,原内容左对齐
print(str_1.ljust(20, "*"))  # 使用*在字符串填充20个长度,原内容左对齐

print(str_1.rjust(20))  # 使用空格在字符串填充20个长度,原内容右对齐
print(str_1.rjust(20, "*"))  # 使用*在字符串填充20个长度,原内容右对齐

print(str_1.zfill(20))  # 在字符串左边填充0,直到长度为20

35-字符串去除内容

str_1 = "    hello world     "
str_2 = "****hello world*****"

print(str_1)
print(str_1.strip())  # 去除字符串首尾的空格(去除字符串两边指定的字符,默认去除空格)

print(str_2)
print(str_2.strip("*"))  # 去除字符串首尾的指定字符

print(str_1.lstrip())  # 去除字符串左侧的空格
print(str_2.lstrip("*"))  # 去除字符串左侧的指定字符

print(str_1.rstrip())  # 去除字符串右侧的空格
print(str_2.rstrip("*"))  # 去除字符串右侧的指定字符

36-字符串分割合并

str_1 = "this is a string expanded...."
str_2 = "this*is*a*string*expanded...."

print(str_1.split())  # 移除字符串首尾的空格(默认空格分割)
print(str_2.split("*"))  # 指定分割符

str_3 = """大法会哦啊福,
艾师傅个人收入付过款,
艾师傅扔掉,
阿斯特热个身,
生日也然后得啊,
"""
print(str_3.splitlines())  # 按照换行符分割

str_4 = '-'
list_1 = ["hello", "world", "world", "nice", "to", "me"]
print(str_4.join(list_1))  # 将列表中的元素以指定的分割符连接成一个字符串
print(str_4.join(["a", "b", "c"]))  # 合并字符串

37-字符串查找

str_1 = "123456789abcdefghijklmnopqwsWAESRTFYUHILOPsdfxhtyfjikgiulkujyhrgrf"

print(str_1.find("A"))  # 查找指定字符串第一次出现的位置,返回索引下标值,如果没有找到,返回-1
print(str_1.find("a"))
print(str_1.find("Q"))  # -1

print(str_1.find("A", 10, 20))  # 从指定范围开始查找,返回索引下标值,如果没有找到,返回-1

print(str_1.index("A"))  # 查找指定字符串第一次出现的位置,返回索引下标值,如果没有找到,会抛出异常

print(str_1.rfind("A"))  # 查找指定字符串最后一次出现的位置,返回索引下标值,如果没有找到,返回-1
print(str_1.rindex("A", 10, 50))  # 从指定范围开始查找,返回索引下标值,如果没有找到,会抛出异常

print(max(str_1))  # 获取字符串中ASCII码值的最大字符
print(min(str_1))  # 获取字符串中ASCII码值的最小字符

38-字符串替换

str_1 = """快科技9月6日消息,近日中国工程院院士刘经南表示,北斗跟华为手机合作的车道级服务可精确到厘米。华为手机,华为手机"""
print(str_1)

print(str_1.replace("刘经南", "**"))  # 指定内容替换,

print(str_1.replace("华为", "**", 3))  # 指定内容,替换内容,替换次数

39-字符串判断

# isupper() 判断字符串中的所有字符都是大写字母,返回布尔值,全部大写返回True,否则返回False
str_1 = "abcdefgABCDEFG123456"
str_2 = "ABCDEFG123456"
print(str_1.isupper())  # False
print(str_2.isupper())  # 全部大写返回True

# islower() 判断字符串中的所有字符都是小写字母,返回布尔值,全部小写返回True,否则返回False
str_3 = "abcdefg123456"
str_4 = "WERTYUIO123456"
print(str_3.islower())  # True
print(str_4.islower())  # False

# isdigit() 判断字符串中的所有字符都是数字,返回布尔值,全部数字返回True,否则返回False
str_5 = "123456"
str_6 = "abcdefg"
print(str_5.isdigit())  # True
print(str_6.isdigit())  # False

# istitle() 判断字符串中的所有字符都是首字母大写,返回布尔值,全部首字母大写返回True,否则返回False
str_7 = "hello world"
str_8 = "Hello World"
print(str_7.istitle())  # False
print(str_8.istitle())  # True

# isalpha() 判断字符串中的内容是字母和文字,返回布尔值,是返回True,否则返回False
str_9 = "123helloworld你好世界"
str_10 = "Hello欢迎"
print(str_9.isalpha())  # False
print(str_10.isalpha())  # True

40-字符串前缀后缀

# startswith() 判断字符串是否以指定字符开头,返回布尔值,是返回True,否则返回False
str_1 = "helloPython"
print(str_1.startswith("hello"))  # True

# endswith() 判断字符串是否以指定字符结尾,返回布尔值,是返回True,否则返回False
print(str_1.endswith("Python"))  # True

41-字符串编码解码

# encode() 字符串以指定进行编码
str_1 = "hello world 你好 世界"
print(str_1.encode("utf-8"))  #
print(str_1.encode("gbk"))

# decode() 字符串以指定进行解码
str_2 = b'hello world \xe4\xbd\xa0\xe5\xa5\xbd \xe4\xb8\x96\xe7\x95\x8c'
str_3 = b'hello world \xc4\xe3\xba\xc3 \xca\xc0\xbd\xe7'
print(str_2.decode("utf-8"))
print(str_3.decode("gbk"))

42-字符串ASCII值转换

# chr() 通过ASCII码值,获得对应的字符
print("ASCII码为:", chr(67))  # 输出:C
print("ASCII码为:", chr(89))  # 输出:Y

# ord() 通过字符,获得对应的ASCII码
print("字符为:", ord("A"))  # 输出:65
print("字符为:", ord("Y"))  # 输出:89

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值