Hello Python
Hello World太俗,来一句Hello Python。
>>> print("Hello Python!") Hello Python!
“Hello Python!”是一个string。
那如果我要写:「”PHP”是最好的语言」呢?
可以使用\转义。
>>> print("\"PHP\"是最好的语言") "PHP"是最好的语言
运算
>>> 3+5 8 >>> print(3+5*5+5-5) 28
这计算器好像还挺好用的。
甚至string都可以和int做乘法运算。
>>> print("a string\n"*3) a string a string a string
不过除了乘法之外都是不行的,浮点数也不行。
>>> print("a string\n"+3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: must be str, not int >>> print("a string\n"-3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'int' >>> print("a string\n"/3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: 'str' and 'int' >>> print("a string\n"*3.1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'float'
不过仔细想想,对string做除了整数乘法的其他运算都感觉怪怪的……