>>> 17/3# classic division returns a float5.666666666666667>>>>>> 17//3# floor division discards the fractional part5>>> 17%3# the % operator returns the remainder of the division2>>> 5*3+2# result * divisor + remainder17
>>> 'spam eggs'# single quotes'spam eggs'>>> 'doesn\'t'# use \' to escape the single quote..."doesn't">>> "doesn't"# ...or use double quotes instead"doesn't">>> '"Yes," he said.''"Yes," he said.'>>> "\"Yes,\" he said."'"Yes," he said.'>>> '"Isn\'t," she said.''"Isn\'t," she said.'
>>> '"Isn\'t," she said.''"Isn\'t," she said.'>>> print('"Isn\'t," she said.')"Isn't," she said.>>> s='First line.\nSecond line.'# \n means newline>>> s# without print(), \n is included in the output'First line.\nSecond line.'>>> print(s)# with print(), \n produces a new lineFirst line.Second line.
如果你不想以 \ 将被解释为特殊字符开头的字符,您可以通过添加 r 使用 原始字符串︰
>>>
>>> print('C:\some\name')# here \n means newline!C:\someame>>> print(r'C:\some\name')# note the r before the quoteC:\some\name
print("""\Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to""")
将生成以下输出(注意,没有开始的第一行):
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
字符串可以用+操作符连接,也可以用*操作符重复多次:
>>>
>>> # 3 times 'un', followed by 'ium'>>> 3*'un'+'ium''unununium'
相邻的两个或多个字符串字面量(用引号引起来的)会自动连接。
>>>
>>> 'Py''thon''Python'
然而这种方式只能用于两个字符串的连接,变量或者表达式是不行的。
>>>
>>> prefix='Py'>>> prefix'thon'# can't concatenate a variable and a string literal ...SyntaxError: invalid syntax>>> ('un'*3)'ium' ...SyntaxError: invalid syntax
如果你想连接多个变量或者连接一个变量和一个字面量,使用“+”:
>>>
>>> prefix+'thon''Python'
这个功能在你想切分很长的字符串的时候特别有用:
>>>
>>> text=('Put several strings within parentheses '... 'to have them joined together.')>>> text'Put several strings within parentheses to have them joined together.'
>>> word[:2]# character from the beginning to position 2 (excluded)'Py'>>> word[4:]# characters from position 4 (included) to the end'on'>>> word[-2:]# characters from the second-last (included) to the end'on'
记住切片如何工作的一种方法是把索引当做字符之间的点,第一个字符的左边是0。含有 n 个字符的字符串的最后一个字符的右边是索引 n,例如:
>>> word[42]# the word only has 6 charactersTraceback (most recent call last):
File "<stdin>", line 1, in <module>IndexError: string index out of range
>>> word[0]='J' ...TypeError: 'str' object does not support item assignment>>> word[2:]='py' ...TypeError: 'str' object does not support item assignment
>>> cubes=[1,8,27,65,125]# something's wrong here>>> 4**3# the cube of 4 is 64, not 65!64>>> cubes[3]=64# replace the wrong value>>> cubes[1, 8, 27, 64, 125]
你还可以通过append ()方法 在列表的末尾添加新的项目(我们将会看到更多相关方法)︰
>>>
>>> cubes.append(216)# add the cube of 6>>> cubes.append(7**3)# and the cube of 7>>> cubes[1, 8, 27, 64, 125, 216, 343]
给切片赋值也是可以的,此操作甚至可以改变列表的大小或者清空它::
>>>
>>> letters=['a','b','c','d','e','f','g']>>> letters['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> # replace some values>>> letters[2:5]=['C','D','E']>>> letters['a', 'b', 'C', 'D', 'E', 'f', 'g']>>> # now remove them>>> letters[2:5]=[]>>> letters['a', 'b', 'f', 'g']>>> # clear the list by replacing all the elements with an empty list>>> letters[:]=[]>>> letters[]