8.从入门到精通:Python 字符串,转义字符,字符串运算符

简介: 8.从入门到精通:Python 字符串,转义字符,字符串运算符

Python 字符串

在 Python 中,字符串是一种基本数据类型,用于表示文本数据。Python 中的字符串是不可变的,即无法修改字符串中的单个字符。
以下是一些关于 Python 字符串的基本知识点:

  • 创建字符串:可以使用单引号(')或双引号(")来创建字符串。如果字符串中包含引号,则需要使用另一种引号来创建字符串。
  • 访问字符串中的字符:可以使用索引来访问字符串中的单个字符。索引从 0 开始,可以使用负数索引从字符串末尾开始计数。
  • 字符串切片:可以使用切片操作符(:)来访问字符串中的子字符串。切片操作符可以指定起始索引和结束索引
  • 字符串操作符:加法(+)可以将两个字符串连接起来,乘法(*)可以将一个字符串重复多次。
  • 字符串方法:Python 中有很多内置的字符串方法,可以用于字符串的处理和操作,例如 split()、replace()、strip()
    等。

以下是一些示例代码,演示了 Python 字符串的基本操作:


创建字符串

a = 'Hello, world!'
b = "I'm a Python programmer."
c = '''This is a multi-line string.
It can span multiple lines.'''
print(a)
print(b)
print(c)

访问字符串中的字符

d = 'Hello, world!'
print(d[0])     # 输出结果为 H
print(d[4])     # 输出结果为 o
print(d[-1])    # 输出结果为 !

字符串切片

e = 'Hello, world!'
print(e[0:5]) # 输出结果为 Hello
print(e[7:])    # 输出结果为 world!

字符串操作符

f = 'Hello, '
g = 'world!'
print(f + g)       # 输出结果为 Hello, world!
print(f * 3)  # 输出结果为 Hello, Hello, Hello,

字符串方法

h = '    Hello, world!    '
print(h.strip())          # 输出结果为 Hello, world!
print(h.replace('world', 'Python')) # 输出结果为 Hello, Python!
print(h.split(','))     # 输出结果为 ['    Hello', ' world!    ']

输出结果为:

Hello, world!
I'm a Python programmer.
This is a multi-line string.
It can span multiple lines.
H
o
!
Hello


Python 转义字符

在 Python 中,转义字符用于在字符串中插入一些特殊字符,例如换行符、制表符、引号等。Python中的转义字符以反斜杠(\)开头,后面跟着一个或多个字符。下面是一些常用的转义字符:


ab7881bb51594111a01ec7716e898ced.png


以下是一些示例代码,演示了 Python 中转义字符的使用:

print('Hello\nworld')   # 使用换行符
print('Hello\tworld')   # 使用制表符
print('I\'m a Python programmer')   # 使用单引号
print("She said, \"I love Python\"")   # 使用双引号

输出结果为:

Hello
world
Hello world
I'm a Python programmer
She said, "I love Python"

需要注意的是,如果要在字符串中使用反斜杠本身,需要使用两个反斜杠来表示,例如:

print('C:\\Users\\Desktop\\file.txt')

输出结果为:

C:\Users\Desktop\file.txt

总之,转义字符在 Python 中是非常有用的,可以帮助我们在字符串中插入一些特殊字符,从而实现更多的功能。


Python字符串运算符

Python中字符串可以使用一些运算符进行操作,以下是一些常见的字符串运算符:

运算符:用于字符串的拼接,将两个字符串连接在一起,形成一个新的字符串。例如:

str1 = 'Hello, '
str2 = 'World!'
str3 = str1 + str2
print(str3) # 输出:Hello, World!

运算符:用于字符串的重复,将一个字符串重复多次,形成一个新的字符串。例如:

str1 = 'Hello, '
str2 = str1 * 3
print(str2) # 输出:Hello, Hello, Hello,

in 运算符:用于判断一个字符串是否包含另一个字符串,返回一个布尔值。例如:

str1 = 'Hello, World!'
print('Hello' in str1) # 输出:True
print('hello' in str1) # 输出:False

not in 运算符:用于判断一个字符串是否不包含另一个字符串,返回一个布尔值。例如:

str1 = 'Hello, World!'
print('Hello' not in str1) # 输出:False
print('hello' not in str1) # 输出:True

% 运算符:用于格式化字符串,将一个字符串中的占位符替换为指定的值。例如:

name = 'Tom'
age = 18
print('My name is %s, and I am %d years old.' % (name, age)) # 输出:My name is Tom, and I am 18 years old.
相关文章
|
14天前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
119 1
|
15天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
200 100
|
15天前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
242 99
|
18天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
18天前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
18天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
29天前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
19天前
|
调度 数据库 Python
Python异步编程入门:asyncio让并发变得更简单
Python异步编程入门:asyncio让并发变得更简单
92 5
|
1月前
|
数据采集 存储 XML
Python爬虫入门(1)
在互联网时代,数据成为宝贵资源,Python凭借简洁语法和丰富库支持,成为编写网络爬虫的首选。本文介绍Python爬虫基础,涵盖请求发送、内容解析、数据存储等核心环节,并提供环境配置及实战示例,助你快速入门并掌握数据抓取技巧。
|
1月前
|
大数据 数据处理 数据安全/隐私保护
Python3 迭代器与生成器详解:从入门到实践
简介:本文深入解析Python中处理数据序列的利器——迭代器与生成器。通过通俗语言与实战案例,讲解其核心原理、自定义实现及大数据处理中的高效应用。
75 0

推荐镜像

更多