字符串
Python中字符串是一种常见的数据类型,用于表示一串字符序列。字符串可以用单引号、双引号或三引号表示。
单引号和双引号用法相同,用于表示一个字符串。例如:
my_string = 'Hello World!'
三引号可以用来表示一个多行字符串,也可以用来表示注释。例如:my_string = '''This is a
multi-line string.'''Python中的字符串是不可变的,这意味着一旦字符串被创建,它的值就不能被修改。但是,可以通过对字符串进行切片、拼接等操作来创建一个新的字符串。
格式化字符串
字符串输出,一般通过print。
# 直接输出
print(123)
print("hello world !")
print(1.5)字符串拼接
简单的字符串拼接:
# 使用+或者,将多个变量或者字符串进行拼接输出,但是需要注意空格和位置等
a = "hello"
b = "world"
print(a + b)
print(a,b)
# 结果
helloworld
hello world
1.5占位符%
常见的占位符:
| 参数 | 意义 |
|---|---|
| %s | 字符串 |
| %f | 浮点数 |
| %d | 整数 |
字符串:
# 一般用法%s,%(连接的内容)
print('你好%s'%'wzs')
输出:你好wzs
# 可以有多个占位符,按照顺序填入
print("hello %s,Do you like %s?"%('wzs','apple'))
输出:hello wzs,Do you like apple?浮点数:
# 使用%f表示,默认显示小数点后6位,也是可以有多个,存在多个指时使用()
print("圆周率是:%f"%3.14159154)
输出:圆周率是:3.141592
# 通过自定义位数显示格式:%.nf,n表示保留多少位小数点
print("圆周率是:%.2f"%3.14159154)
输出:圆周率是:3.14整数:
# 使用%d表示整数,也可以表示浮点数,只保留整数位
print("I eat %d apples"%10)
输出:I eat 10 apples
fomat格式化
# 可以使用format()方法对输出进行格式化。
格式:print("hello {},Do you like {}?".format('wzs','apple'))
有多个占位按顺序匹配后面的字符,默认从0开始
输出:hello wzs,Do you like apple?
# 也可以指定顺序
print("hello {1},Do you like {0}?".format('wzs','apple'))
输出:hello apple,Do you like wzs?
# {}里面可以带参数设置,可以设置位置居中以及填充字符,{0:^|<|>}居中,左对齐,右对齐,冒号:后面可以设置重复的字符串,只能一个
print("我是{0},我喜欢数字{1:-<8}".format("spy",'5'))
输出:我是spy,我喜欢数字5-------
print("我是{0},我喜欢数字{1:-^8}".format("spy",'5'))
输出:我是spy,我喜欢数字---5----
print("我是{0},我喜欢数字{1:*^8}".format("spy",'5'))
输出:我是spy,我喜欢数字***5****
# 也可以通过指定key,来设置对应的值
print("My name is {name},I like eat {food}".format(name='chuck',food='beef'))
输出:My name is chuck,I like eat beef
# 可以使用列表下标指定输入
names = ['chuck','wzs']
print("Hello {names[0]},My name is {names[1]} !".format(names = names))
输出:Hello chuck,My name is wzs !
# 千分位分隔
print("{:,}".format(124356475687))
输出:124,356,475,687f-string
在字符串前加上f,然后使用{}添加表达式,可以在表达式调用变量进行计算。
# 表达式计算
n = 2
print(f"2的平方等于 {n*n}。")
输出:2的平方等于 4。
# 格式化浮点数,和占位符类似使用.nf
val = 3.1415923
print(f'{val:.2f}')
print(f'{val:.5f}')
输出:
3.14
3.14159
# 字符串对齐,使用:^|<|>n表示
f1 = 'a'
f2 = 'ab'
f3 = 'abc'
f4 = 'abcd'
print(f'{f1:>10}')
print(f'{f2:>10}')
print(f'{f3:>10}')
print(f'{f4:>10}')
输出:
a
ab
abc
abcd
# 主要是对于输出的位数已经对齐反向进行设置
空格
lstrip和rstrip方法
lstrip()
# 使用方法将字符串的左边的空白隐藏
a = " python "
print(a.lstrip())
输出:
pythonrstrip()
# 使用方法将右边空白隐藏
a = " python "
print(a.rstrip())
输出:
pythonstrip()
# 将两端空白进行隐藏。格式化英文字母输出
常见的是对英文字符进行大小写格式化。
title()大写开头字母、upper()全部字母大写、lower()全部字母小写。
# title()方法
lanuge = "python"
print(lanuge.title())
输出:Python
# upper()方法
lanuge = "python"
print(lanuge.upper())
输出:PYTHON
# lower()方法
lanuge = "PYTHON"
print(lanuge.lower())
输出:python内置方法
split()方法,对字符串进行切片,默认以空格对字符串进行切片,返回切片后的列表。
test_str = 'Hello world i like joan !'
print(test_str.split())
# 输出
['Hello', 'world', 'i', 'like', 'joan', '!']
# 可以指定分隔符和切片次数
test_str = 'Hello world, i like joan !'
print(test_str.split(','))
# 指定切片次数
test_str = 'Hello world i like joan !'
print(test_str.split(' ',2))
# 输出
['Hello', 'world', 'i like joan !']join()方法,使用字符串拼接字符串。
# 格式为str.join(sequence),str为指定字符串,sequence为字符串或者列表等
list1 = ['chuck','joan']
print('-'.join(list1))
1 游客 2025-03-28 00:11 回复
555
1 游客 2025-03-28 00:10 回复
555-1 OR 438=(SELECT 438 FROM PG_SLEEP(15))--
1 游客 2025-03-28 00:09 回复
5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z
1 游客 2025-03-28 00:08 回复
555
1 游客 2025-03-28 00:07 回复
555
@@oHGrt 游客 2025-03-28 00:05 回复
555
-1' OR 2+979-979-1=0+0+0+1 -- 游客 2025-03-28 00:03 回复
555
-1 OR 3+256-256-1=0+0+0+1 游客 2025-03-28 00:03 回复
555
-1 OR 2+256-256-1=0+0+0+1 游客 2025-03-28 00:03 回复
555
-1 OR 3+940-940-1=0+0+0+1 -- 游客 2025-03-28 00:03 回复
555
-1 OR 2+940-940-1=0+0+0+1 -- 游客 2025-03-28 00:03 回复
555
1 游客 2025-03-28 00:03 回复
555
1 游客 2025-03-28 00:02 回复
555
1 游客 2025-03-28 00:00 回复
555
1 游客 2025-03-28 00:00 回复
555
1 游客 2025-03-27 23:59 回复
555