转义字符
1.转义字符的使用
>>> 'Let's go!'
SyntaxError: invalid syntax
**>>> 'Let\'s go!'**
"Let's go!"
2.如果不使用转义需要拼接:
>>> "Let's say " '"Hello, world!"'
'Let\'s say "Hello, world!" '
>>> x = "Hello, "
>>> y = "world!"
>>> x y
SyntaxError: invalid syntax
>>> "Hello, " + "world!"
'Hello, world!'
>>> x = "Hello, "
>>> y = "world!"
>>> x + y
'Hello, world!'
3.字符串表示 str 和 repr(str是一个类,但repr是一个函数)
>>> "Hello,\nworld!"
'Hello,\nworld!'
>>> print("Hello,\nworld!")
Hello,
world!
>>> print(repr("Hello,\nworld!"))
'Hello,\nworld!'
>>> print(str("Hello,\nworld!"))
Hello,
world!
4.长字符串和原始字符串
print('''This is a very long string. It continues here.
And it's not over yet. "Hello, world!"
Still here.''')
"""like this"""
>>>print("Hello, \ world!")
Hello, world!
>>> 1 + 2 + \
4 + 5
12
>>> print('C:\\nowhere')
C:\nowhere
>>> print(r'C:\Program Files\fnord\foo\bar\baz\frozz\nozz')
C:\Program Files\fnord\foo\bar\baz\frozz\nozz
原始字符串不能以单个反斜杠结尾
>>> print(r"This is illegal\")
SyntaxError: EOL while scanning string literal
可以这样解决:
>>> print(r'C:\Program Files\foo\bar' '\\')
C:\Program Files\foo\bar\
5.基本函数
abs(number) 返回指定数的绝对值
bytes(string, encoding[, errors]) 对指定的字符串进行编码,并以指定的方式处理错误
cmath.sqrt(number) 返回平方根;可用于负数
float(object) 将字符串或数字转换为浮点数
help([object]) 提供交互式帮助
input(prompt) 以字符串的方式获取用户输入
int(object) 将字符串或数转换为整数
math.ceil(number) 以浮点数的方式返回向上圆整的结果
math.floor(number) 以浮点数的方式返回向下圆整的结果
math.sqrt(number) 返回平方根;不能用于负数
pow(x, y[, z]) 返回x的y次方对z求模的结果
print(object, ...) 将提供的实参打印出来,并用空格分隔
repr(object) 返回指定值的字符串表示
round(number[, ndigits]) 四舍五入为指定的精度,正好为5时舍入到偶数
str(object) 将指定的值转换为字符串。用于转换bytes时,可指定编码和错误处理方式