目录
字符串也被称为“不可变的字符序列”,其可以使用 单引号、双引号、三引号 及 """ """ 来定义,来定义:
注意:三引号 及 """ """ 定义的字符串可分布在连续多行
- a = 'Welcome to Python' # 单引号
- b = "Welcome to Python" # 双引号
-
- c = '''Welcome to
- Python'''
- d = """Welcome to
- Python"""
-
- print(a)
- print(b)
- print(c)
- print(d)
-
- >> Welcome to Python
- >> Welcome to Python
- >> Welcome to
- >> Python
- >> Welcome to
- >> Python
我们常见的整数类型一般都是十进制的,但它其实还有常用的二进制、八进制、十六进制的表达方式,其中:
二进制 —>以0b开头,用bin()进行转换
- print(bin(10))
-
- >> 0b1010
八进制 —>以0o开头,用oct()进行转换
- print(oct(100))
-
- >> 0o144
十六进制 —>以0x开头,用hex()进行转换
- print(hex(1000))
-
- >> 0x3e8
注意: 以上述方法进行类型转换之后,我们的转换后的整数……会因此变为字符串类型
- print(bin(10), type(bin(10)))
- print(oct(100), type(oct(100)))
- print(hex(1000), type(hex(1000)))
-
- >> 0b1010 <class 'str'>
- >> 0o144 <class 'str'>
- >> 0x3e8 <class 'str'>
当然,我们也可以从 二、八、十六进制转回十进制:
- a = '1010'
- b = '144'
- c = '3e8'
- print(int(a, 2)) # 二转十
- print(int(b, 8)) # 八转十
- print(int(c, 16)) # 十六转十
-
- >> 10
- >> 100
- >> 1000
注意: 此时我们无需加上开头的0b、0o或0x,但转换前他们必须为字符串类型
- from pythonds.basic import Stack
-
-
- def convert(decNumber, base):
- num = "0123456789ABCDEF"
- remstack = Stack()
- while decNumber > 0:
- rem = decNumber % base
- remstack.push(rem)
- decNumber //= base
-
- binString = ""
- while not remstack.isEmpty():
- binString += num[remstack.pop()]
-
- return binString
-
-
- n, m = map(int, input().split())
- if m == 2:
- print(f"0b{convert(n, m)}")
- elif m == 8:
- print(f"0o{convert(n, m)}")
- else:
- print(f"0x{convert(n, m)}")
浮点数由整数部分和小数部分组成,同时其具有存储的不精确性(例如下面的1.1 + 2.2)
- a = 3.1415
- print(a, type(a))
-
- print(1.1 + 2.2)
-
- >> 3.1415 <class 'float'>
- >> 3.3000000000000003
解决方法: 用模块decimal
- from decimal import Decimal
-
- print(Decimal('1.1') + Decimal('2.2'))
-
- >> 3.3
用来表示真或假的值,True为真,False为假。布尔值可以转化为整数:True —> 1,False —> 0
- a = True
- b = False
- print(a, type(a))
- print(b, type(b))
-
- print(a + 1)
- print(b + 1)
-
- >> True <class 'bool'>
- >> False <class 'bool'>
- >> 2
- >> 1
Why do we need 类型转换?答:最基本的是,为了将不同数据类型的数据拼接起来。
例如:
- name = 'cheems'
- age = 16
-
- print('My dog ' + name + ' is ' + age + ' years old')
-
- print('My dog ' + name + ' is ' + str(age) + ' years old')
第一个输出必然报错:TypeError: can only concatenate str (not "int") to str
问题就出在age为int变量,因此利用str()函数转化后 —> 输出二,可以正常输出
将其他类型转为字符串
- a = 16
- b = True
- c = 3.14
-
- print(str(a), type(str(a)))
- print(str(b), type(str(b)))
- print(str(c), type(str(c)))
-
- >> 16 <class 'str'>
- >> True <class 'str'>
- >> 3.14 <class 'str'>
同时,我们也可以用引号进行转换(无论是单引号、双引号、三引号 或 """ """)
- print('123', type('123'))
- print("True", type("True"))
- print('''3.14''', type('''3.14'''))
- print("""123""", type("""123"""))
-
- >> 123 <class 'str'>
- >> True <class 'str'>
- >> 3.14 <class 'str'>
- >> 123 <class 'str'>
注意:(1)文字类和小数类字符串均无法转化成整数;(2)浮点数转为整数时,抹零取整(不进行四舍五入)。
- a = '16'
- b = True
- c = 3.14
- d = '3.14'
- e = 'Python'
-
- print(int(a), type(int(a)))
- print(int(b), type(int(b)))
- print(int(c), type(int(c)))
- print(int(d), type(int(d)))
- print(int(e), type(int(e)))
-
- >> 16 <class 'int'>
- >> 1 <class 'int'>
- >> 3 <class 'int'>
- >> ValueError: invalid literal for int() with base 10: '3.14'
- >> ValueError: invalid literal for int() with base 10: 'Python'
注意:(1)文字类字符串均无法转化成浮点数;(2)整数转为浮点数时,末尾添上.0。
- a = '16'
- b = True
- c = 114514
- d = '3.14'
- e = 'Python'
-
- print(float(a), type(float(a)))
- print(float(b), type(float(b)))
- print(float(c), type(float(c)))
- print(float(d), type(float(d)))
- print(float(e), type(float(e)))
-
- >> 16.0 <class 'float'>
- >> 1.0 <class 'float'>
- >> 114514.0 <class 'float'>
- >> 3.14 <class 'float'>
- >> ValueError: could not convert string to float: 'Python'