python中字符串的使用

本文深入讲解Python中的字符串定义、遍历、统计、判断、查找、替换、文本对齐、拆分与拼接等操作,通过实例演示每种方法的使用技巧。

一、字符串定义和遍历

str1 = "hello python"

str2 = '我的外号是"大西瓜"'

print(str2)

print(str1[6])

for char in str2:

    print(char)

 

二、字符串统计操作

hello_str = "hello hello"

# 1. 统计字符串长度

print(len(hello_str))

# 2. 统计某一个小(子)字符串出现的次数

print(hello_str.count("llo"))

print(hello_str.count("abc"))

# 3. 某一个子字符串出现的位置

print(hello_str.index("llo"))

# 注意:如果使用index方法传递的子字符串不存在,程序会报错!

print(hello_str.index("abc"))

 

三、字符串判断方法

hello_str = "hello world"

# 1. 判断是否以指定字符串开始

print(hello_str.startswith("Hello"))

# 2. 判断是否以指定字符串结束

print(hello_str.endswith("world"))

# 3. 查找指定字符串

# index同样可以查找指定的字符串在大字符串中的索引

print(hello_str.find("llo"))

# index如果指定的字符串不存在,会报错

# find如果指定的字符串不存在,会返回-1

print(hello_str.find("abc"))

# 4. 替换字符串

# replace方法执行完成之后,会返回一个新的字符串

# 注意:不会修改原有字符串的内容

print(hello_str.replace("world", "python"))

print(hello_str)

 

四、字符串的查找与替换

hello_str = "hello world"

# 1. 判断是否以指定字符串开始

print(hello_str.startswith("Hello"))

# 2. 判断是否以指定字符串结束

print(hello_str.endswith("world"))

# 3. 查找指定字符串

# index同样可以查找指定的字符串在大字符串中的索引

print(hello_str.find("llo"))

# index如果指定的字符串不存在,会报错

# find如果指定的字符串不存在,会返回-1

print(hello_str.find("abc"))

# 4. 替换字符串

# replace方法执行完成之后,会返回一个新的字符串

# 注意:不会修改原有字符串的内容

print(hello_str.replace("world", "python"))

print(hello_str)

 

五、字符串的文本对齐

# 假设:以下内容是从网络上抓取的

# 要求:顺序并且居中对齐输出以下内容

poem = ["\t\n登鹳雀楼",

         "王之涣",

         "白日依山尽\t\n",

         "黄河入海流",

         "欲穷千里目",

         "更上一层楼"]

for poem_str in poem:

    # 先使用strip方法去除字符串中的空白字符

    # 再使用center方法居中显示文本

    print("|%s|" % poem_str.strip().center(10, " "))

 

六、字符串的拆分与拼接

# 假设:以下内容是从网络上抓取的

# 要求:

# 1. 将字符串中的空白字符全部去掉

# 2. 再使用 " " 作为分隔符,拼接成一个整齐的字符串

poem_str = "登鹳雀楼\t 王之涣 \t 白日依山尽 \t \n 黄河入海流 \t\t 欲穷千里目 \t\t\n更上一层楼"

print(poem_str)

# 1. 拆分字符串

poem_list = poem_str.split()

print(poem_list)

# 2. 合并字符串

result = " ".join(poem_list)

print(result)

 

七、for语法的演练

for num in [1, 2, 3]:

    print(num)

    if num == 2:

        break

else:

    # 如果循环体内部使用break退出了循环

    # else 下方的代码就不会被执行

    print("会执行吗?")

print("循环结束")

Python中的字符串是一种不可变的序列类型,用于表示文本数据。字符串可以通过单引号(')、双引号(")或三引号('''或""")来创建。三引号字符串可以跨越多行,常用于多行字符串和注释。 字符串Python中是序列的一种,所以它支持一些通用的序列操作,比如索引、切片、乘法和成员资格测试等。 下面是字符串的一些常见操作: 1. 索引与切片:通过索引可以访问字符串中的特定字符,通过切片可以获取字符串的一部分。 ```python s = "Hello, world!" print(s[0]) # 输出 'H' print(s[1:5]) # 输出 'ello' ``` 2. 字符串连接:可以使用加号(+)来连接两个字符串。 ```python s1 = "Hello" s2 = "world" print(s1 + ", " + s2) # 输出 'Hello, world' ``` 3. 重复:使用乘法操作符(*)可以重复字符串。 ```python print("Python" * 3) # 输出 'PythonPythonPython' ``` 4. 成员资格测试:使用in和not in来检查某个字符串是否包含在另一个字符串中。 ```python print('H' in "Hello") # 输出 True print('z' not in "Python") # 输出 True ``` 5. 转义字符:在字符串中可以使用反斜杠(\)来引入特殊字符,如换行(\n)、制表符(\t)等。 ```python print("Hello\nPython") # 输出 'Hello' 后跟一个换行,然后是 'Python' ``` 6. 原始字符串:在字符串前加上前缀r或R表示原始字符串,它不会处理字符串中的转义字符。 ```python print(r"\n") # 输出 '\n' 而不是换行 ``` 7. 字符串方法:Python提供了许多字符串方法,例如upper(), lower(), split(), replace(), find(), format()等,用于处理字符串数据。 ```python s = "hello, world" print(s.upper()) # 输出 'HELLO, WORLD' print(s.split(",")) # 输出 ['hello', ' world'] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值