美文网首页
Python处理输入输出小技巧

Python处理输入输出小技巧

作者: 一个扫地的垃圾 | 来源:发表于2020-08-06 13:23 被阅读0次

    1. 输入

    1.1 输入字符串"简书"

    x1 = input()
    print(x1, type(x1))
    

    输出:

    jianshu
    jianshu <class 'str'>
    

    1.2 输入整数123

    x2 = int(input())
    print(x2, type(x2))
    

    输出:

    123
    123 <class 'int'>
    

    1.3 输入字符串数组jian shu

    x3 = input().split()
    print(x3, type(x3))
    

    输出:

    jian shu
    ['jian', 'shu'] <class 'list'>
    

    1.4 输入整型数组1 2 3

    x4 = input()
    x4 = [int(n) for n in x4.split()]
    print(x4, type(x4))
    

    输出:

    1 2 3
    [1, 2, 3] <class 'list'>
    

    2. 输出

    2.1 输出字符串数组

    y1 = ["jian", "shu"]
    y1 = " ".join(y1)
    print(y1, type(y1))
    

    输出:

    jian shu <class 'str'>
    

    2.2 输出整型数组

    y2 = [1, 2, 3]
    y2 = " ".join(str(n) for n in y2)
    print(y2, type(y2))
    

    输出:

    1 2 3 <class 'str'>
    

    3. 倒序输出

    3.1 倒序输出字符串

    z1 = "python"
    z1 = z1[::-1]  # 倒序
    print(z1, type(z1))
    

    输出:

    nohtyp <class 'str'>
    

    3.2 倒序输出整型数组

    z2 = [1, 2, 3]
    z2 = z2[::-1]  # 倒序
    z2 = " ".join([str(n) for n in z2])
    print(z2, type(z2))
    

    输出:

    3 2 1 <class 'str'>
    

    相关文章

      网友评论

          本文标题:Python处理输入输出小技巧

          本文链接:https://www.haomeiwen.com/subject/jopzrktx.html