• 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]
  • 相关阅读:
    山东第一届省赛1001 Phone Number(字典树)
    HD2222 Keywords Search(AC自动机入门题)
    POJ 1947Rebuilding Roads(树形DP + 01背包)
    zoj 3946 Highway Project(最短路 + 优先队列)
    HDU5672String(尺标法)
    HDU5671Matrix(矩阵行列交换)
    HDU5670Machine(抽象进制)
    用户体验评价
    团队冲刺第二阶段-6
    第十四周学习进度
  • 原文地址:https://www.cnblogs.com/virusdefender/p/3370570.html
Copyright © 2020-2023  润新知