我最近遇到了python f字符串。让我们有一长串像
这样的字符串string = 'L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWScalA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1pQH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTtTvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMOK9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrxSdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qqfPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4dmjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJOUubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB'
我要像这样打印此字符串:
L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWSc alA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1p QH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTt TvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMO K9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrx SdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qq fPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl 9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4d mjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJO UubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB
我想使用F弦。我已经尝试过像
print(f"{string}:<{40}")
但是它不起作用。
答案 0 :(得分:2)
这是使用textwrap的目的:
import textwrap
string = 'L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWScalA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1pQH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTtTvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMOK9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrxSdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qqfPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4dmjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJOUubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB'
# print("{string}:<40")
print(textwrap.fill(string, 40))
L4qrGFHK8eDA9Vy05gNH7inxfaVpPZH3i9pRdWSc
alA0pIGHKGqUEXejplKiYaCNizbiKH72LoQaoz1p
QH9caDfAX5xtfQAZEri7QGkvxMAJWXsjPEPLQhTt
TvvhDR1tMM9zX8Dd0l15bBW1Q3VQReOsbP5AQmMO
K9GV0WYPZ015sg7tg8JKOs7hFJfD8bdpUQgWbGrx
SdS95PnBTf4P2nTWWLrWzo3DQNyXHs29R6MZ92qq
fPLGL8SSQNchWyo4V9NoHdAHDo5TdPf6VmNQaQAl
9HKLVawTTg379plHr81YYEoojzstCSPh3jAy9W4d
mjTLrBUxzA9tK5UlHKMGx7IYieNGfXBKTaCegdJO
UubZPajkp0KY8OcpHxlaVFVdIPi58n6VH7evAomB