【Python】字符串 合并 切片 转换 格式化 拆分组合 find index count strip

2.2 字符串

可用单引号、双引号、三引号。

字符串是一种不可变序列。

1)合并、重复

result = "hello" * 3 + "world" * 2
print(result)  # 输出:"hellohellohelloworldworld"

2)切片、索引

  • 索引:从0开始,可为负数。

  • 切片:

    str[start:end:step]

    倒叙:str[::-1]

3)字符串转换

  • 数字转字符串:

    str(20)

  • 字符串转数字:

    int(‘32’) float(‘1.2’)

    不允许 int(‘3.2’)

  • 大小写字母转换

    1.upper()方法

    将字符串中所有字母变为大写。

    text = "Hello, World!"
    print(text.upper())
    # 输出结果为: HELLO, WORLD!
    

2.lower()方法

将字符串中所有字母变为小写。

text = "Hello, World!"
print(text.lower())
# 输出结果为: hello, world!

3.capitalize()方法

将字符串首字母大写,其他字符小写。

text = "hello, world!"
print(text.capitalize())
# 输出结果为: Hello, world!

4.title()方法

将字符串中每个单词的首字母都大写。

text = "hello, world!"
print(text.title())
# 输出结果为: Hello, World!

4)字符串格式化

* 用%进行格式化

name = "John"
age = 30
print("My name is %s and I'm %d years old." % (name, age))
# 输出结果为:My name is John and I'm 30 years old.

一般只要记住%s和%d就好了,这两个占位符的使用频率最高

符号 描述

%C 格式化字符及其ASCII码

%s 格式化字符串

%d 格式化整数

%u 格式化无符号整型

%o 格式化无符号八进制数

%X 格式化无符号十六进制数

%X 格式化无符号十六进制数(大写)

%f 格式化浮点数字,可指定小数点后的精度

%e 用科学计数法格式化浮点数

%E 作用同%e,用科学计数法格式化浮点数

%g %f和%e的简写

%G %F和%E的简写

%p 用十六进制数格式化变量的地址

* format()

name = "John"
age = 30
print("My name is {} and I'm {} years old.".format(name, age))
# 输出结果为:My name is John and I'm 30 years old.

* 用 f-string格式化

name = "John"
age = 30
print(f"My name is {name} and I'm {age} years old.")
# 输出结果为:My name is John and I'm 30 years old.

5)字符串的拆分和组合

* split()

通过 split() 方法可以将一个字符串按照指定的分隔符进行拆分,返回一个由拆分后的子串组成的列表。

text = "apple,banana,orange"
fruits = text.split(",")
print(fruits)
# 输出结果为:['apple', 'banana', 'orange']

* join()

通过 join() 方法可以将一个字符串序列(如列表或元组)中的所有元素组合成一个新的字符串并返回。

fruits = ['apple', 'banana', 'orange']
text = ",".join(fruits)
print(text)
# 输出结果为:apple,banana,orange

6)find、index、count、strip

* find()

用于在字符串中查找指定子字符串第一次出现的位置,并返回其索引值。

如果没有匹配的字符串,则返回 -1。

str.find(sub[, start[, end]])

'''
str 表示要搜索的字符串
sub 表示要查找的子字符串
start 和 end 表示查找范围的起始位置和结束位置(可选参数)。
'''
  • s = "hello, world"
    index = s.find("world")
    print(index)
    # 输出结果为:7
    

* index()

index() 方法与 find() 方法类似,都是用于在字符串中查找指定子字符串第一次出现的位置,并返回其索引值。

不同的是,如果没有匹配的字符串,则会抛出 ValueError 异常。

str.index(sub[, start[, end]])

s = "hello, world"
try:
    index = s.index("world")
except ValueError:
    index = -1
print(index)
# 输出结果为:7

* count()

计算字符串中指定子字符串出现的次数,并返回计数值。

str.count(sub[, start[, end]])

s = "hello, world"
count = s.count("o")
print(count)
# 输出结果为:2

* strip()

strip() :去除字符串两端的空白字符(包括空格、制表符和换行符),并返回处理后的字符串。

lstrip():去除左侧字符

rstrip():去除右侧字符。

  • 注:strip() 方法不会修改原始字符串,而是返回一个新字符串。
str.strip([chars])
str.lstrip([chars])
str.rstrip([chars])


s = "  hello, world\t\n"
new_s = s.strip()
print(new_s)
# 输出结果为:"hello, world"
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值