Python处理输入输出小技巧

1. 输入

1.1 输入字符串""

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

输出:

jianshu
jianshu 

1.2 输入整数123

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

输出:

123
123 

1.3 输入字符串数组jian shu

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

输出:

jian shu
['jian', 'shu'] 

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] 

2. 输出

2.1 输出字符串数组

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

输出:

jian shu 

2.2 输出整型数组

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

输出:

1 2 3 

3. 倒序输出

3.1 倒序输出字符串

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

输出:

nohtyp 

3.2 倒序输出整型数组

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

输出:

3 2 1 

你可能感兴趣的:(Python处理输入输出小技巧)