9.Python从入门到精通—Python 字符串格式化,三引号,Unicode 字符串

9.Python从入门到精通—Python 字符串格式化,三引号,Unicode 字符串

Python 字符串格式化

Python中的字符串格式化是指将一个字符串中的占位符替换为指定的值。Python中有多种字符串格式化的方法,以下是其中的几种常见方法:

使用百分号(%)进行字符串格式化

使用百分号(%)进行字符串格式化是Python中最早的字符串格式化方法。它的基本语法如下:

"格式化字符串" % (1,2, ...)

其中,格式化字符串中使用占位符来表示需要替换的位置,占位符以%开始,后面跟着一个或多个字符,表示需要替换的值的类型。常见的占位符有:

%d:整数
%f:浮点数
%s:字符串
%c:字符
%x:十六进制整数

例如:

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.

使用format()方法进行字符串格式化

使用format()方法进行字符串格式化是Python中常用的字符串格式化方法之一。它的基本语法如下:

"格式化字符串".format(1,2, ...)

其中,格式化字符串中使用占位符来表示需要替换的位置,占位符以{}表示。如果需要在占位符中指定值的类型,可以在{}中使用冒号(:)进行格式化。例如:

name = 'Tom'
age = 18
print('My name is {}, and I am {} years old.'.format(name, age))

输出结果为:

My name is Tom, and I am 18 years old.

使用f-string进行字符串格式化

使用f-string进行字符串格式化是Python 3.6及以上版本中新增的字符串格式化方法。它的基本语法如下:
f"格式化字符串"

其中,格式化字符串中使用占位符来表示需要替换的位置,占位符以{}表示。如果需要在占位符中指定值的类型,可以在{}中使用冒号(:)进行格式化。例如:

name = 'Tom'
age = 18
print(f'My name is {
      
      name}, and I am {
      
      age} years old.')

输出结果为:

My name is Tom, and I am 18 years old.

Python 三引号

在 Python 中,三引号用于创建多行字符串,可以使用单引号或双引号来表示。三引号中的文本可以包含换行符、引号等特殊字符,而无需使用转义字符。以下是一些示例代码,演示了 Python 中三引号的使用:

a = '''This is a multi-line string.
It can span multiple lines.'''
print(a)

b = """This is another multi-line string.
It can also span multiple lines."""
print(b)

输出结果为:

This is a multi-line string.
It can span multiple lines.
This is another multi-line string.
It can also span multiple lines.

需要注意的是,三引号中的文本会保留原始格式,包括空格和换行符。

如果不希望保留原始格式,可以使用字符串方法 strip() 来删除首尾的空格和换行符。例如:

c = '''    This is a multi-line string.
    It can span multiple lines.    
    '''
print(c.strip())

输出结果为:

This is a multi-line string.
It can span multiple lines.

Unicode 字符串

在 Python 中,Unicode 字符串是一种特殊的字符串类型,用于表示任意字符集中的字符。Unicode 字符串以“u”开头,例如 u’Hello, world!'。在 Python 3 中,所有字符串都是 Unicode 字符串。

Unicode 字符串可以包含任何 Unicode 字符,包括 ASCII 字符和非 ASCII 字符。Unicode字符串可以使用转义序列来表示任意 Unicode 字符,例如 \uXXXX 和 \UXXXXXXXX,其中 XXXX 和 XXXXXXXX
分别表示 Unicode 字符的十六进制编码。

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

创建 Unicode 字符串

a = u'Hello, world!'
b = u'你好,世界!'
c = u'\u2603\u2764\u2600'  # 表示雪花、心形和太阳的 Unicode 字符

print(a)
print(b)
print(c)

输出结果为:


```python
Hello, world!
你好,世界!
☃❤☀

需要注意的是,Python 2 中默认的字符串类型是 ASCII 字符串,如果需要使用 Unicode字符串,需要在字符串前面添加“u”前缀。而在 Python 3 中,所有字符串都是 Unicode 字符串,无需添加前缀。
总之,Unicode 字符串在 Python 中是非常有用的,可以帮助我们处理各种字符集中的字符。

Python 的字符串内建函数

Python中的字符串内建函数是指可以直接使用的与字符串相关的函数,以下是一些常见的字符串内建函数:

len():返回字符串的长度。

capitalize():将字符串的第一个字符转换为大写字母。

lower():将字符串中的所有字符转换为小写字母。

upper():将字符串中的所有字符转换为大写字母。

title():将字符串中每个单词的首字母大写。

swapcase():将字符串中的大小写字母互换。

count():返回字符串中指定子字符串的出现次数。

find():查找字符串中指定子字符串的位置,如果找到返回第一次出现的位置,否则返回-1。

index():查找字符串中指定子字符串的位置,如果找到返回第一次出现的位置,否则会抛出异常。

replace():将字符串中指定的子字符串替换为另一个字符串。

split():将字符串按照指定的分隔符分割成多个子字符串,并返回一个列表。

strip():去除字符串两端的空格或指定字符。

join():将多个字符串拼接成一个字符串。

isdigit():判断字符串是否只包含数字字符。

isalpha():判断字符串是否只包含字母字符。

isspace():判断字符串是否只包含空格字符。

isupper():判断字符串中所有字母是否都是大写字母。

islower():判断字符串中所有字母是否都是小写字母。

startswith():判断字符串是否以指定的子字符串开头。

endswith():判断字符串是否以指定的子字符串结尾。

# len() 示例
s = "Hello, World!"
print(len(s))  # 输出:13

# capitalize() 示例
s = "hello, world!"
print(s.capitalize())  # 输出:"Hello, world!"

# lower() 示例
s = "Hello, World!"
print(s.lower())  # 输出:"hello, world!"

# upper() 示例
s = "Hello, World!"
print(s.upper())  # 输出:"HELLO, WORLD!"

# title() 示例
s = "hello, world!"
print(s.title())  # 输出:"Hello, World!"

# swapcase() 示例
s = "Hello, World!"
print(s.swapcase())  # 输出:"hELLO, wORLD!"

# count() 示例
s = "hello, world! hello, world!"
print(s.count("hello"))  # 输出:2

# find() 示例
s = "hello, world!"
print(s.find("world"))  # 输出:7

# index() 示例
s = "hello, world!"
print(s.index("world"))  # 输出:7

# replace() 示例
s = "hello, world!"
print(s.replace("world", "Python"))  # 输出:"hello, Python!"

# split() 示例
s = "hello,world,Python"
print(s.split(","))  # 输出:['hello', 'world', 'Python']

# strip() 示例
s = "   hello, world!   "
print(s.strip())  # 输出:"hello, world!"

# join() 示例
s1 = "hello"
s2 = "world"
print("-".join([s1, s2]))  # 输出:"hello-world"

# isdigit() 示例
s = "12345"
print(s.isdigit())  # 输出:True

# isalpha() 示例
s = "hello"
print(s.isalpha())  # 输出:True

# isspace() 示例
s = "   "
print(s.isspace())  # 输出:True

# isupper() 示例
s = "HELLO"
print(s.isupper())  # 输出:True

# islower() 示例
s = "hello"
print(s.islower())  # 输出:True

# startswith() 示例
s = "hello, world!"
print(s.startswith("hello"))  # 输出:True

# endswith() 示例
s = "hello, world!"
print(s.endswith("world!"))  # 输出:True

猜你喜欢

转载自blog.csdn.net/weixin_50804299/article/details/136801676