python的输出和输入形式

输出~~~~~~~~

 

第一种就是最常用的print函数了,在py2中是 print str, 在py3中就是print(str)

但是python中就是print函数输出的时候会自动换行,比如

str = "123456"
for i in range(len(str)):
#注意别漏了range
    print str[i]

输出就成了

>>>
1
2
3
4
5
6
>>>这样的了

Python 2:使用print后加一个逗号:print 'hello',
Python 3:输入参数end:print ('hello', end='')

但是这样呢,每个元素之间都会自动加一个空格的。

 

最终的解决办法就是

 

import sys
str = "123456"
for i in range(len(str)):
    sys.stdout.write(str[i])

 

 

 

输入~~~~

Python 2.3.4 (#1, Feb  2 2005, 11:44:13)   
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2  
Type "help", "copyright", "credits" or "license" for more information.  
>>> user=raw_input("please input:")         
please input:wei                          #  raw_input 输入  字符串  成功  
>>> user  
'wei'  
>>> user=input("please input:")            
please input:123                          #  input 输入  数字  成功(返回的是数字)  
>>> user  
123  
>>> user=raw_input("please input:")  
please input:111 <span style="white-space:pre">          #  raw_input 输入  数字  成功(返回的还是当成字符串)  
>>> user  
'111'  
>>> user=input("please input:")  
please input:wei                          #  input  输入字符串   失败  
Traceback (most recent call last):  
  File "<stdin>", line 1, in ?  
  File "<string>", line 0, in ?  
NameError: name 'wei' is not defined  

在python 2.7中一样

 

在python3中 就这样了

Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win  
32  
Type "help", "copyright", "credits" or "license" for more information.  
>>> user=raw_input("please input:")                 #没有了raw_input  
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
NameError: name 'raw_input' is not defined  
>>> user=input("please input:")  
please input:wei  
>>> user  
'wei'  
>>> user=input("please input:")                     #input的输出结果都是作为字符串  
please input:123  
>>> user  
'123'  

所以输入数字的时候就要转换为int了

 

输入多个参数的时候要这样

reply=raw_input("input")
pieces=reply.split()
print pieces[0]
print pieces[1]

 

转载于:https://www.cnblogs.com/virusdefender/p/3370570.html

Python输入输出有多种方式。一种方式是使用print()函数来输出值。另一种方式是使用文件对象的write()方法来输出值。如果你希望输出形式更加多样,可以使用str.format()函数来格式化输出值。如果你希望将输出的值转成字符串,可以使用repr()或str()函数来实现。另外,使用input()函数可以获得用户的输入。通过将input()函数的返回值赋值给一个变量,你可以获取用户输入的字符串。例如,str1 = input()将会将用户输入的字符串赋值给变量str1。你也可以使用文件对象的read()和readline()接口来从文件中读取字符串并重构为原来的Python对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python输入输出](https://blog.csdn.net/weixin_51884452/article/details/129723862)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Python基础知识部分——输入输出](https://blog.csdn.net/x568059888/article/details/105905875)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值