Int(整数)
- 在
Python
中定义变量是 不需要指定类型 (在其他高级语言中都需要) - 整数就是数学中的数字
- 整型在Python中不受长度限制大小范围
- 使用
type
函数可以查看一个变量的类型
In [1]: 1
Out[1]: 1
In [2]: type(1)
Out[2]: int
浮点型
In [1]: 1.0
Out[1]: 1.0
In [2]: type(1.0)
Out[2]: float
类型转化
浮点数转化为整数(显示转化)
In [3]: int(1.0)
Out[3]: 1
In [4]: float(1)
Out[4]: 1.0
隐式转化,系统背后默认进行转换
In [5]: 1/2
Out[5]: 0.5
字符串
字符串创建
- 字符串是
Python
中常用的数据类型。我们可以使用引号(‘’ 或 “”)来创建字符串 - 创建字符串很简单,只要为变量一个值即可。
- 单引号和双引号
- 在
Python
中单引号与双引号都可以用来表示一个字符串
print("What's your name?")
结果:
"What's your name?"
print('"python"是一门优秀的语言')
结果:
"python"是一门优秀的语言
一个单引号并不是单引号,它是字符串创建的格式
# 三引号实现块注释
""" 文档注释
三引号实现块注释
"""
''''''
字符串格式化
- 把不是字符类型的转化变成字符串
- 在Python中可以使用
print
函数将信息输出到控制台 - 如果希望输出文字信息的同时,一起输出 数据,就需要使用到 格式化操作符
format
- 此函数可以快速的处理各种字符串,增强了字符串格式化的功能。基本语法是使用{}和:来替代%
name = '张三'
age = 18
nickname = '法外狂徒'
# format 用 {} 占位
print('姓名:{},年龄{},外号:{} '.format(name, age, nickname))
print('hello {} 你今年已经{}岁了'.format(name, age))
# 保留小数点后两位
In [1]: '{:.2f}'.format(12.333)
Out[1]: '12.33'
In [2]: '{a:.2f}'.format(a=12.333)
Out[2]: '12.33'
In [3]: '{a:6.2f}'.format(a=12.333)
Out[3]: ' 12.33'
%s
%
被称为 格式化操作符,专门用于处理字符串中的格式- 包含
%
的字符串,被称为 格式化字符串 %
和不同的 字符 连用,不同类型的数据 需要使用 不同的格式化字符
格式化字符 | 含义 |
---|
%s | 字符串 |
%d | 有符号十进制整数,%06d 表示输出的整数显示位数,不足的地方使用 0 补全 |
%f | 浮点数,%.2f 表示小数点后只显示两位 |
% % | 浮点数,%.2f 表示小数点后只显示两位 |
print("格式化字符串 %s" % 变量1)
print("格式化字符串" % (变量1, 变量2...))
name = '张三'
age = 18
nickname = '法外狂徒'
name2 = '李四'
age2 = 19
nickname2 = '帮凶'
# %s 用 %s 占位
print('姓名:%s' % name)
# 多个参数
print('%s,%s 哦嗨呦' % (name, name2))
格式化字符 | 含义 |
---|
%s | 字符串 |
%d | 有符号十进制整数,%06d 表示输出的整数显示位数,不足的地方使用 0 补全 |
%f | 浮点数,%.2f 表示小数点后只显示两位 |
% % | 输出 % |
%c | %ASCII字符 |
%o | %8进制 |
%x | %16进制 |
%e | %科学计数法 |
f
- f’{}'形式,并不是真正的字符串常量,而是一个运算求值表达式,可以很方便的用于字符串拼接、路径拼接等
name = '张三'
# f 在字符串中嵌入变量
print(f'hello {name} !')
字符串常用方法
- 字符串是不可变数据类型,所有的操作都是返回新的一个列表,不会修改原有列表
- 字符串 能够使用的 方法
In [1]: hello_str.
hello_str.capitalize hello_str.isidentifier hello_str.rindex
hello_str.casefold hello_str.islower hello_str.rjust
hello_str.center hello_str.isnumeric hello_str.rpartition
hello_str.count hello_str.isprintable hello_str.rsplit
hello_str.encode hello_str.isspace hello_str.rstrip
hello_str.endswith hello_str.istitle hello_str.split
hello_str.expandtabs hello_str.isupper hello_str.splitlines
hello_str.find hello_str.join hello_str.startswith
hello_str.format hello_str.ljust hello_str.strip
hello_str.format_map hello_str.lower hello_str.swapcase
hello_str.index hello_str.lstrip hello_str.title
hello_str.isalnum hello_str.maketrans hello_str.translate
hello_str.isalpha hello_str.partition hello_str.upper
hello_str.isdecimal hello_str.replace hello_str.zfill
hello_str.isdigit hello_str.rfind
查找和替换
方法 | 用法 |
---|
string.strip() | 默认去掉 string 左右两边的空白字符 |
string.replace(old_str, new_str) | 把 string 中的 old_str 替换成 new_str |
string.split() | 默认以空白字符进行分割 |
string.join(seq) | 将 seq 中所有的元素(字符串类型)合并为一个新的字符串 |
hello = "hello world !"
print(hello.replace('world', 'Python'))
print(hello)
- 字符串是一种不可变的数据类型。所有操作字符的方法都是返回一个新的字符串。可以用返回的新的字符串覆盖掉之前的字符串
转义字符
- 在需要在字符中使用特殊字符时,python 用反斜杠转义字符
转义字符 | 描述 |
---|
\ | (在行尾时)续行符 |
\ | 反斜杠符号 |
\' | 单引号 |
\" | 双引号 |
\a | 响铃 |
\b | 退格(Backspace) |
\e | 转义 |
\000 | 空 |
\n | 换行 |
\v | 纵向制表符 |
\t | 横向制表符 |
\r | 回车 |
\f | 换页 |
\oyy | 八进制数,yy代表的字符,例如:\o12代表换行 |
\xyy | 十六进制数,yy代表的字符,例如:\x0a代表换行 |
\other | 其它的字符以普通格式输出 |
原始字符串
- 由于字符串中的反斜线都有特殊的作用,因此当字符串中包含反斜线时,就需要使用转义字符 \ 对字符串中包含的每个 ‘’ 进行转义。
- 比如说,我们要写一个关于 Windows 路径
G:\xxx\codes\02\2.4
这样的字符串,如果在 Python 程序中直接这样写肯定是不行的,需要使用 \ 转义字符,对字符串中每个 ‘’ 进行转义,即写成 G:\\xxx\\codes\\02\\2.4
这种形式才行 - 原始字符串以“r”开头,它不会把反斜线当成特殊字符
# 原始文件路径
a="G:\xxx\codes\02\2.4"
new_a="G:\\xxx\\codes\\02\\2.4"
# 原始字符串包含的引号,同样需要转义
s2 = r'"Let\'s go", said Charlie'
print(s2)
布尔类型(bool)
- python 中布尔值使用常量 True 和 False来表示;注意大小写
bool
是 int
的子类(继承 int
),故 True == 1 False == 0
是会返回 Ture
bool
类型只有两种状态真或假
In [17]: bool(-1)
Out[17]: True
In [18]: bool(0)
Out[18]: False
In [19]: bool(None)
Out[19]: False
In [20]: bool("")
Out[20]: False
In [21]: bool(" ")
Out[21]: True
In [22]: def a():
...: pass
...:
...: bool(a)
Out[22]: True
- True 对 False 错
- 布尔类型只有两种状态,
True
or Flase
。数字除了零之外,其余均为 True
,字符串除了空字符串之外都为True
,对象除了空对象之外,其余均为 True
- 默认情况下,所有类型都可以转化为布尔类型