python基础(2):print()
2017-09-20 15:13
190 查看
print 字符串
python 中 print 字符串 要加”或者”“>>> print('hello world') ''' hello world ''' >>> print("hello world 2") ''' hello world 2 '''
print 字符串叠加
可以使用 + 将两个字符串链接起来, 如以下代码.>>> print('Hello world'+' Hello Hong Kong') """ Hello world Hello Hong Kong """
简单运算
可以直接print 加法+,减法-,乘法*,除法/. 注意:字符串不可以直接和数字相加,否则出现错误。>>> print(1+1) """ 2 """ >>> print(3-1) """ 2 """ >>> print(3*4) """ 12 """ >>> print(12/4) """ 3.0 """ >>> print('iphone'+4) #字符串不可以直接和数字相加 """ Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> print('iphone'+4) TypeError: Can't convert 'int' object to str implicitly """
int() 和 float();当int()一个浮点型数时,int会保留整数部分,比如 int(1.9),会输出1,而不是四舍五入。
>>> print(int('2')+3) #int为定义整数型 """ 5 """ >>> print(int(1.9)) #当int一个浮点型数时,int会保留整数部分 """ 1 """ >>> print(float('1.2')+3) #float()是浮点型,可以把字符串转换成小数 """" 4.2 """"
相关文章推荐
- Python3基础 print中+号连接两个字符串,字符串的拼接
- Python基础1语言介绍+数据类型+print语句
- 2.Python - print、variable、基础数学运算
- Python基础教程代码与注释P65 5.1 print和import的更多信息
- python基础教程共60课-第2课print
- leetcode-1:python基础,循环、条件、列表、字典的应用,enumerate、range函数以及2.0和3.0print的区别
- Python3基础 __str__ print一个类的实例对象的时候 使用的魔法方法
- Python基础-print
- Python3基础 print括号中 字符串乘以一个整数 输出多个相同的字符串
- python基础1--------print(输出)
- python 基础知识-----print多次输入不换行
- Python3基础 str + 字符串变量拼接后,用print输出
- Python基础-调试print()-assert-logging-pdb-ide
- [Python]基础语法(一)print
- Python3基础 print 用反斜杠输出单引号与双引号
- Python2.7刷基础Day1----关于Print
- Python基础入门(四)- Print语句
- Python基础 print()莫烦视频
- Python3基础 在print中用 %d 输出一个整数
- Python3基础 print 输出helloworld