Python 3 介绍(九)-- Python字符串

目录

1. 字符串的表示

示例:

2. 字符串的创建

使用单引号:

使用双引号:

3. 字符串的转义字符

示例:

4. 字符串的拼接

示例:

5. 字符串的重复

示例:

6. 字符串的索引和切片

示例:

7. 字符串的方法

7.1 len() 函数

7.2 str.lower() 和 str.upper()

7.3 str.strip()、str.lstrip() 和 str.rstrip()

7.4 str.split() 和 str.join()

7.5 str.replace()

7.6 str.startswith() 和 str.endswith()

7.7 str.find() 和 str.index()

8. 字符串格式化

8.1 使用 % 操作符

8.2 使用 str.format() 方法

8.3 使用 f-string (Python 3.6+)

9. 字符串的编码

示例:

总结


 

在 Python 3 中,字符串是一种非常常用的数据类型,用于表示文本信息。字符串可以包含字母、数字、符号以及其他字符。Python 中的字符串是不可变的,这意味着一旦创建了一个字符串,就不能改变其内容。但是,可以创建新的字符串来替代旧的字符串。

1. 字符串的表示

字符串可以用单引号 (' ') 或双引号 (" ") 来定义。Python 并不区分这两种表示方式,选择哪种完全取决于个人偏好。

示例:

 

python

深色版本

1single_quoted = 'This is a string'
2double_quoted = "This is also a string"

2. 字符串的创建

使用单引号:

 

python

深色版本

1greeting = 'Hello, world!'

使用双引号:

 

python

深色版本

1message = "Hello, again!"

3. 字符串的转义字符

如果字符串中包含单引号或双引号等特殊字符,可以使用转义字符 \ 来表示。

示例:

 

python

深色版本

1escaped_quotes = "He said, \"Hello, world!\""
2escaped_newline = "First line\nSecond line"

4. 字符串的拼接

可以使用 + 操作符来连接(拼接)两个字符串。

示例:

 

python

深色版本

1first_name = "John"
2last_name = "Doe"
3full_name = first_name + " " + last_name
4print(full_name)  # 输出 "John Doe"

5. 字符串的重复

可以使用 * 操作符来重复一个字符串。

示例:

 

python

深色版本

1repeat_string = "abc" * 3
2print(repeat_string)  # 输出 "abcabcabc"

6. 字符串的索引和切片

字符串支持索引和切片操作,可以访问字符串中的单个字符或子串。

示例:

 

python

深色版本

1text = "Python"
2print(text[0])  # 输出 'P'
3print(text[-1]) # 输出 'n'
4print(text[1:4])  # 输出 "yth"
5print(text[:3])  # 输出 "Pyt"
6print(text[3:])  # 输出 "hon"

7. 字符串的方法

Python 提供了丰富的字符串方法,可以用来处理字符串。

7.1 len() 函数

获取字符串的长度。

 

python

深色版本

1text = "Python"
2print(len(text))  # 输出 6

7.2 str.lower() 和 str.upper()

将字符串转换为小写或大写。

 

python

深色版本

1text = "Python"
2print(text.lower())  # 输出 "python"
3print(text.upper())  # 输出 "PYTHON"

7.3 str.strip()str.lstrip() 和 str.rstrip()

去除字符串两端的空白字符。

 

python

深色版本

1text = "   Python   "
2print(text.strip())  # 输出 "Python"

7.4 str.split() 和 str.join()

拆分字符串为列表,或使用指定的分隔符将列表中的字符串连接起来。

 

python

深色版本

1words = "Python is fun".split()
2print(words)  # 输出 ['Python', 'is', 'fun']
3
4sentence = '-'.join(words)
5print(sentence)  # 输出 "Python-is-fun"

7.5 str.replace()

替换字符串中的子串。

 

python

深色版本

1text = "Python is fun"
2new_text = text.replace("fun", "awesome")
3print(new_text)  # 输出 "Python is awesome"

7.6 str.startswith() 和 str.endswith()

检查字符串是否以指定的子串开头或结尾。

 

python

深色版本

1text = "Python is fun"
2print(text.startswith("Python"))  # 输出 True
3print(text.endswith("fun"))  # 输出 True

7.7 str.find() 和 str.index()

查找子串在字符串中的位置。

 

python

深色版本

1text = "Python is fun"
2index = text.find("is")
3print(index)  # 输出 7
4
5# `index` 方法会在找不到子串时抛出 ValueError 异常
6index = text.index("is")
7print(index)  # 输出 7

8. 字符串格式化

Python 提供了多种字符串格式化的方法。

8.1 使用 % 操作符

 

python

深色版本

1name = "Alice"
2age = 30
3print("My name is %s and I am %d years old." % (name, age))

8.2 使用 str.format() 方法

 

python

深色版本

1name = "Alice"
2age = 30
3print("My name is {} and I am {} years old.".format(name, age))

8.3 使用 f-string (Python 3.6+)

 

python

深色版本

1name = "Alice"
2age = 30
3print(f"My name is {name} and I am {age} years old.")

9. 字符串的编码

Python 3 中默认使用 UTF-8 编码来处理字符串。可以使用 .encode() 方法将字符串编码为字节串,使用 .decode() 方法将字节串解码为字符串。

示例:

 

python

深色版本

1text = "Python"
2encoded_text = text.encode('utf-8')
3print(encoded_text)  # 输出 b'Python'
4
5decoded_text = encoded_text.decode('utf-8')
6print(decoded_text)  # 输出 'Python'

总结

Python 3 中的字符串是一个非常强大的数据类型,提供了丰富的操作方法和功能。通过了解和掌握字符串的各种操作,可以有效地处理文本数据,并在实际编程中发挥重要作用。无论是简单的文本处理还是复杂的字符串匹配和替换,Python 的字符串都能满足需求。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值