What is the Python code to split up a string so that it prints out normally in an 80-character window without wrapping? [closed]
When I run my program (which decrypts a paragraph from a certain document), I have:
W
E
T
H
E
P
E
O
P
L
E
O
F
T
H
E
U
N
I
T
E
D
S
T
A
T
E
S
I
N
O
R
D
E
R
T
O
F
O
R
M
A
M
O
R
E
P
E
R
F
E
C
T
U
N
I
O
N
E
S
T
A
B
L
I
S
H
J
U
S
T
I
C
E
I
N
S
U
R
E
D
O
M
E
S
T
I
C
T
R
A
N
Q
U
I
L
I
T
Y
P
R
O
V
I
D
E
F
O
R
T
H
E
C
O
M
M
O
N
D
E
F
E
N
S
E
P
R
O
M
O
T
E
T
H
E
G
E
N
E
R
A
L
W
E
L
F
A
R
E
A
N
D
S
E
C
U
R
E
T
H
E
B
L
E
S
S
I
N
G
S
O
F
L
I
B
E
R
T
Y
T
O
O
U
R
S
E
L
V
E
S
A
N
D
O
U
R
P
O
S
T
E
R
I
T
Y
D
O
O
R
D
A
I
N
A
N
D
E
S
T
A
B
L
I
S
H
T
H
I
S
C
O
N
S
T
I
T
U
T
I
O
N
F
O
R
T
H
E
U
N
I
T
E
D
S
T
A
T
E
S
O
F
A
M
E
R
I
C
A
You can't tell from here but basically it is one letter per line.
However, I want it to say:
WE THE PEOPLE OF THE UNITED STATES, IN ORDER TO FORM A MORE PERFECT UNION, ESTABLISH JUSTICE, INSURE DOMESTIC TRANQUILITY, PROVIDE FOR THE COMMON DEFENSE, PROMOTE THE GENERAL WELFARE, AND SECURE THE BLESSINGS OF LIBERTY TO OURSELVES AND OUR POSTERITY, DO ORDAIN AND ESTABLISH THIS CONSTITUTION FOR THE UNITED STATES OF AMERICA.
What is the code I must add to my program in order for this to happen?
I don't know what that one-character-a-line is about because you didn't tell us the reason, but the textwrap module will give you what you want:
s="WE THE PEOPLE OF THE UNITED STATES, IN ORDER TO FORM A MORE PERFECT UNION, ESTABLISH JUSTICE, INSURE DOMESTIC TRANQUILITY, PROVIDE FOR THE COMMON DEFENSE, PROMOTE THE GENERAL WELFARE, AND SECURE THE BLESSINGS OF LIBERTY TO OURSELVES AND OUR POSTERITY, DO ORDAIN AND ESTABLISH THIS CONSTITUTION FOR THE UNITED STATES OF AMERICA."
import textwrap
print "\n".join(textwrap.wrap(s, 80))
I reconstructed your original code from your comment, and this is a corrected version:
# You don't even use this so why import it? --> import string
def main():
user_string = raw_input()
all_caps = user_string.upper() # guess you wanted to make it uppercase
output = [] # this will hold the decoded characters
for char in all_caps:
if char.isalpha():
value = ord(char)
if 70 <= value <= 90: # look at this, almost no other programming language supports that syntax
num = value - 5
elif 65 <= value <= 69:
num = value + 21
output.append(chr(num)) # add the decoded character to the output list
else:
output.append(char) # add the character verbatim to the output list (e.g. whitespace)
print "".join(output) # print out the list by putting it together into a string
main()
Do you do something like this:
for char in string:
print char
? If yes, fix that to:
for char in string:
print char,
the comma(,
) at the end of the line omits the newline print
normally prints.
But even that is probably not what you want (since it prints a space after each char), the following code should fix that, too:
import sys
for char in string:
sys.stdout.write(char)
print s[0],
i = 1
for char in s[1:]:
if i%80:
print "\bchar",
else:
print "\b\n"
i += 1
精彩评论