一、字符串
字符串(string)就是一系列字符。在Python中,用引号引起来的都是字符串,可以是单引号,也可以是双引号。
1.修改字符串大小写
方法(method)是Python可对数据的操作。
① .title
str = "this is string example....wow!!!";
print("str.title() :" , str.title())
输出:str.title() : This Is String Example....Wow!!!
- 在
str.title
中,str
后面的句点(.)让Python对str
变量执行title()
方法指定的操作。 title()
方法以首字母大写的方式显示每个单词,即将每个单词的首字母改为大写。
② .upper
str = "this is string example....wow!!!";
print("str.upper() :" , str.upper())
输出:str.upper() : THIS IS STRING EXAMPLE....WOW!!!
upper
方法将字符串改为全大写
③ .lower
str = "this is string example....wow!!!";
print("str.lower() :" , str.lower())
输出:str.lower() : this is string example....wow!!!
lower
方法将字符串改为全小写
2.f 字符串
f是format(设置格式)的简写,要在字符串中插入变量的值,可先在左引号前加上字母f,再将要插入的变量放在花括号内。
first_name='Payton'
last_name='von'
full_name=f"{first_name} {last_name}"
print(full_name)
print(f"Hello,{full_name}")
print(f"Hello,{full_name.title()}")
输出:
Payton von
Hello,Payton von
Hello,Payton Von
3.删除空白与前缀
①strip()
name1=' Payton '
print(name1.strip())
print(name1.rstrip())
print(name1.lstrip())
输出:
Payton
Payton
Payton (有空白)
strip
删除字符串两端的空白rstrip
删除字符串右边的空白lstrip
删除字符串左边的空白
②removeprefix()
huangse = "https://blog.csdn.net/m0_74970386?spm=1001.2015.3001.5343"
sssss=huangse.removeprefix("https://")
print(huangse)
print(sssss)
输出:
https://blog.csdn.net/m0_74970386?spm=1001.2015.3001.5343
blog.csdn.net/m0_74970386?spm=1001.2015.3001.5343
- removeprefix()方法:在变量名后边加上句点和removeprefix()方法,并且在括号内输入了想要从原始字符串中删除的前缀。
- removesuffix()方法:删除后缀
二、数
1.整数
- integer,可以对其进行加(+)减(-)乘(*)除(/)操作
- 乘方操作(**)
3**2=9
2.浮点数
- python将带小数点的数称为浮点数(float)——小数点可以出现在数的任何位置上。
- 任意两个数相除,结果总是浮点数
- 其他任何运算中,只要有一个操作数是浮点数,结果总为浮点数
3.常量与数中的下划线
- 在书写很大的数时,可以使用下划线将其中的位分组,存储这种数时,Python会忽略其中的下划线
huangse = 666_666_666
print(huangse)
输出:666666666
- 常量(constant)是在程序的整个生命周期内都保持不变的变量。
- Python没有内置的常量类型,一般约定变量名全大写,即为常量。