博主往期文章分享:
【机器学习】专栏
http://t.csdnimg.cn/YSi6y
【Python学习】专栏http://t.csdnimg.cn/ckqMs
一、基本字符串方法
(一)、 字符串大小写转换
- str.capitalize()
将字符串的第一个字符转换为大写,其余字符转换为小写。
- str.upper()
将字符串中的所有字符转换为大写。
- str.lower()
将字符串中的所有字符转换为小写。
- str.title()
将每个单词的首字母转换为大写,其余字母转换为小写。
text = "hello world"
print(text.upper()) # 输出: HELLO WORLD
print(text.lower()) # 输出: hello world
print(text.capitalize()) # 输出: Hello world
print(text.title()) # 输出: Hello World
(二)、 字符串切片和分割
- str.split()
根据指定的分隔符将字符串分割成列表。
sentence = "one,two,three,four"
print(sentence[0:3]) # 输出: one
print(sentence.split(',')) # 输出: ['one', 'two', 'three', 'four']
(三)、 字符串查找和替换
- str.replace(old, new)
将字符串中的旧子串替换为新子串,可选参数 count 指定替换次数。
- str.find()
分别返回子串首次出现的位置或抛出异常(如果未找到)。
message = "Hello there, General Kenobi!"
print(message.find("there")) # 输出: 6
print(message.replace("Kenobi", "Skywalker")) # 输出: Hello there, General Skywalker!
(四)、 字符串对齐和填充
str.rjust(width[, fillchar])
此方法将字符串右对齐,并使用指定的字符(默认为空格)填充左侧,直到达到指定的总宽度。
str.ljust(width[, fillchar])
与此类似,该方法将字符串左对齐,并在右侧使用指定的字符填充,直到达到指定的总宽度。
number = "42"
print(number.rjust(5, '0')) # 输出: 00042
print(number.ljust(5, '*')) # 输出: 42***
(五)、 字符串格式化
- str.format()
使用 {} 占位符进行字符串格式化。
- f-string (formatted string literals)
在字符串前加 f 或 F,并在 {} 中引用变量或表达式。
name = "Ada Lovelace"
age = 26
print(f"{name} is {age} years old.") # 输出: Ada Lovelace is 26 years old.
print("{} is {} years old.".format(name, age)) # 输出: Ada Lovelace is 26 years old.
(六)、 字符串判断
- str.isalnum(), str.isalpha(), str.isdigit(), str.islower(), str.isupper(), str.isspace(), str.istitle()
分别检查字符串是否全部由字母数字、字母、数字、小写字母、大写字母、空格或标题格式组成。
test = "123"
print(test.isdigit()) # 输出: True
print(test.isalpha()) # 输出: False
(七)、字符串去除空白
- str.strip(), str.lstrip(), str.rstrip()
分别移除字符串两端、左边或右边的空白字符或指定字符。
greeting = " hello "
print(greeting.strip()) # 输出: hello
(八)、字符串连接
- str.join(iterable)
使用指定的字符串连接一个元素序列。
parts = ["Python", "is", "awesome"]
print(' '.join(parts)) # 输出: Python is awesome
(九)、字符串编码和解码
- str.encode(encoding='utf-8', errors='strict')
将字符串编码为字节序列。
- bytes.decode(encoding='utf-8', errors='strict')
将字节序列解码为字符串。
encoded = "Python".encode('utf-8')
print(encoded) # 输出: b'Python'
decoded = encoded.decode('utf-8')
print(decoded) # 输出: Python
二、整型(int
)
Python的整型(int
)是用于处理整数值的数据类型。虽然Python中的整型不像某些其他语言那样有固定的位数限制(例如32位或64位),而是可以存储任意大小的整数,只要可用内存足够。以下是一些与整型相关的常用处理方法:
(一)、 创建整型
整型可以直接通过数字字面量创建,也可以通过int()
函数从其他类型转换而来。
# 直接创建整型
a = 10
# 从字符串转换
b = int("123")
# 从浮点数转换,会向下取整
c = int(123.45)
(二)、数学运算
整型支持各种算术运算,如加法、减法、乘法、除法、取模、幂等。
x = 10
y = 3
print(x + y) # 加法
print(x - y) # 减法
print(x * y) # 乘法
print(x / y) # 浮点数除法
print(x // y) # 整数除法
print(x % y) # 取模
print(x ** y) # 幂
(三)、比较和逻辑运算
整型支持比较运算符和逻辑运算符。
a = 5
b = 10
print(a < b) # 输出: True
print(a == b) # 输出: False
print(bool(a)) # 输出: True
print(bool(0)) # 输出: False
(四)、格式化和转换
可以将整型转换为字符串或其他进制表示。
n = 10
print(str(n)) # 转换为字符串
print(bin(n)) # 转换为二进制字符串
print(oct(n)) # 转换为八进制字符串
print(hex(n)) # 转换为十六进制字符串
(五)、 位操作
整型支持位运算,如按位与、按位或、按位异或、按位取反和位移。
a = 5 # 二进制: 0101
b = 3 # 二进制: 0011
print(a & b) # 按位与
print(a | b) # 按位或
print(a ^ b) # 按位异或
print(~a) # 按位取反
print(a << 2) # 左移
print(a >> 2) # 右移
(六)、 随机数生成
可以使用random
模块生成随机整数。
import random
print(random.randint(1, 10)) # 生成1到10之间的随机整数
(七)、数学函数
Python的math
模块提供了许多数学函数,可以应用于整型。
import math
print(math.factorial(5)) # 计算阶乘
print(math.gcd(12, 18)) # 计算最大公约数