1. 输出

>>> print "hello world"
hello world
>>> print 'hello world'
hello world
>>> print 'Hello','world'
Hello world
>>> print "I'm studing python"
I'm studing python
>>> print 'I am studing "python"'
I am studing "python"
>>> print "I'm studing \"python\""
I'm studing "python"
>>> print 'I\'m studing "python"'
I'm studing "python"
>>> print '''I'm studing "python"'''
I'm studing "python"
>>> print '''I am
... studing
... "python"'''
I am
studing
"python" >>> print "Name:%s Age:%d Height:%f" %('Tester',18,1.80)
Name:Tester Age:18 Height:1.800000
>>> print "Name:%8s Age:%3d Height:%5.2f" %('Tester',18,1.80)
Name: Tester Age: 18 Height: 1.80
>>> print "Name:%-8s Age:%-3d Height:%-5.2f" %('Tester',18,1.80)
Name:Tester Age:18 Height:1.80
>>> print "Name:%s Age:%3d Height:%*.*f" % ('Tester',18,6,2,1.80)
Name:Tester Age: 18 Height: 1.80
print "Name:%r Age:%r Height:%r" %('Tester',18,1.80)
Name:'Tester' Age:18 Height:1
>>> print '12',3
12 3
>>> print '12'+3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print '12'+'3'
123
>>> print 12+3
15

  

总结:
1) print 输出字符串可以使用''或者""表示;
2) print后可以有多个输出,以逗号分隔,最终逗号以空格输出,两边类型可以不一致;
也可以用+号连接,但是+号要求两边类型必须一致,连接数字表示相加,连接字符串表示相连;
3) 如果字符串本身包含"",可以使用''来表示;如果字符串本身包含'',可以使用""来表示;
4) 如果字符串本身既包含''也包含"",可以使用\进行转义;常用转义字符:\n 换行; \t制表符; \\ 表示\本身
5) 如果字符串本身既包含''也包含"",也可以使用'''来表示;'''一般用于输出多行文本中;
6) 格式化输出可以指定输出字段的长度,%5.2f表示输出浮点数的小数点后有2位,总共长度为5位,默认为右对齐,不足的左侧补空格;
%-5.2f表示左对齐,长度不足的右边补空格
7) 对于格式未定的字段,可以通过类似%*.*f % (5,2,1.80)来设定表示%5.2f
8) 对于类型不确定的字段输出可以统一用%r表示,%r的格式化指定格式后面再学习。

2. format输出

1) 接受位置参数输出

>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra'

2) 接受参数名称输出

>>> print "His name is {name} and age is {age} ".format(age=18,name='Tom')
His name is Tom and age is 18

3) 接受对象属性输出

>>> class person():
... def __init__(self):
... self.name='Jack'
... def get_name(self):
... print 'his name is {self.name}'.format(self=self)
>>> p.get_name()
his name is Jack

4) 接受序列的数据输出

>>> a=('tom','Jack','meng')
>>> print '{0[0]},{0[1]},{0[2]}'.format(a)
tom,Jack,meng

5) 格式化输出,可代替%s,%r

>>> print 'His name is {}'.format('tom')
His name is tom
>>> print 'His name is {:10}'.format('tom')
His name is tom
>>> print 'His name is {:<10}'.format('tom')
His name is tom
>>> print 'His name is {:>10}'.format('tom')
His name is tom
>>> print 'His name is {:^10}'.format('tom')
His name is tom
>>> print 'His name is {:*<10}'.format('tom')
His name is tom*******
>>> print 'His name is {:.>10}'.format('tom')
His name is .......tom
>>> print 'His name is {:#^10}'.format('tom')
His name is ###tom####
>>> print 'His name is {:#10}'.format('tom')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Alternate form (#) not allowed in string format specifier
>>> print '%08.2f'%(1.15)
00001.15
>>> print '%8.2f'%(1.15)
1.15
>>> print '{:8.2f}'.format(1.15)
1.15

说明: 指定填充字符适合必须要指明对齐方向

3. input / raw_input输入

>>> raw_var = raw_input("please input your content:")
please input your content:Testing python
>>> print raw_var
Testing python
>>> type(raw_var)
<type 'str'>
>>>
>>> var = input("please input your content:")
please input your content:'Testing python'
>>> print var
Testing python
>>> type(var)
<type 'str'>
>>> var = input("please input your content:")
please input your content:Testing python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
Testing python
^
SyntaxError: unexpected EOF while parsing
>>> >>> raw_var = raw_input("please input your content:")
please input your content:100+300
>>> print raw_var
100+300
>>>
>>> var = input("please input your content:")
please input your content:100+300
>>> print var
4
>>> age = input('please input your age:')
please input your age:18
>>> print age
18
>>> type(age)
<type 'int'>

  

总结:
1) raw_input将所有输入作为字符串看待,返回也是字符串类型
2) input 没有将所有输入作为字符串看待,所以在输入字符串内容时候需要添加引号,否则会出错
3) input可以接收表达式,并返回表达式结果
4)input返回也不一定是字符串类型;如果接收的是带引号的字符串那么返回的才是字符串类型;

学习1:python输入输出的更多相关文章

  1. Python学习 Part5:输入输出

    Python学习 Part5:输入输出 1. 格式化输出 三种输出值的方法: 表达式语句 print()函数 使用文件对象的write()方法 两种方式格式化输出: 由自己处理整个字符串,通过使用字符 ...

  2. Python语言学习之Python入门到进阶

    人们常说Python语言简单,编写简单程序时好像也确实如此.但实际上Python绝不简单,它也是一种很复杂的语言,其功能特征非常丰富,能支持多种编程风格,在几乎所有方面都能深度定制.要想用好Pytho ...

  3. 从零学习基于Python的RobotFramework自动化

    从零学习基于Python的RobotFramework自动化 一.        Python基础 1)      版本差异 版本 编码 语法 其他 2.X ASCII try: raise Type ...

  4. 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

    算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

  5. Python学习(一) Python安装配置

    我本身是Java程序猿,听说Python很强大,所以准备学习一下Python,虽说语言都是相同的,但java跟python肯定还是有区别的.希望在此记录一下自己的学习过程. 目前,Python分2.X ...

  6. 学习《Python核心编程》做一下知识点提要,方便复习(一)

    学习<Python核心编程>做一下知识点提要,方便复习. 计算机语言的本质是什么? a-z.A-Z.符号.数字等等组合成符合语法的字符串.供编译器.解释器翻译. 字母组合后产生各种变化拿p ...

  7. Android模拟器使用笔记,学习head_first python 安卓开发章节

    学习head_first python 安卓开发那一章需要的程序android-sdk_r23.0.2-windows.zip //模拟器 PythonForAndroid_r4.apk sl4a_r ...

  8. Python 数据分析(二 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识

    Python 数据分析(二) 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识 第1节 groupby 技术 第2节 数据聚合 第3节 分组级运算和转换 第4 ...

  9. 每个程序员都应该学习使用Python或Ruby

    每个程序员都应该学习使用Python或Ruby 如果你是个学生,你应该会C,C++和Java.还会一些VB,或C#/.NET.多少你还可能开发过一些Web网页,你知道一些HTML,CSS和JavaSc ...

  10. ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库

    ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库. 与现有的主要针对监督任务设计的深度学习库不同,ZhuSuan 的特点是深入到贝叶斯推理中,从而支持各种生成模式:传 ...

随机推荐

  1. 优先队列实现 大小根堆 解决top k 问题

      摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...

  2. 【题解】CF#855 G-Harry Vs Voldemort

    个人感觉挺有意思的,然而被颜神D无聊惹(- ̄▽ ̄)- 这题我们可以首先试图去统计以每一个点作为 w 点所能对答案造成的贡献是多少.不难发现,当且仅当 u 和 v 都在 w 所在边双的一侧的时候不能构成 ...

  3. CF484E Sign on Fence && [国家集训队]middle

    CF484E Sign on Fence #include<bits/stdc++.h> #define RG register #define IL inline #define _ 1 ...

  4. BZOJ1509 & 洛谷4408:[NOI2003]逃学的小孩——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1509 https://www.luogu.org/problemnew/show/P4408 sb ...

  5. 洛谷4525 & 4526:【模板】自适应辛普森法——题解

    参考:https://phqghume.github.io/2018/05/19/%E8%87%AA%E9%80%82%E5%BA%94%E8%BE%9B%E6%99%AE%E6%A3%AE%E6%B ...

  6. [Leetcode] word search 单词查询

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. Android Studio中进行单元测试

    写单元测试类 1.创建单元测试文件夹,即新建一个用于单元测试的包,存放单元测试的类. 2.创建一个类如 ExampleTest,注意要继承自InstrumentationTestCase类. 3.创建 ...

  8. Linux查看内核和系统版本

    1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@crowe.d ...

  9. LightOJ 1028 - Trailing Zeroes (I) 质因数分解/排列组合

    题意:10000组数据 问一个数n[1,1e12] 在k进制下有末尾0的k的个数. 思路:题意很明显,就是求n的因子个数,本来想直接预处理欧拉函数,然后拿它减n就行了.但注意是1e12次方法不可行.而 ...

  10. Create MSSQL Procedure

    代码: CREATE PROCEDURE [dbo].[sp_UpdateCouponCount] AS GO