Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

Printing List as a String doesn't print all the values

I am trying to Print the List of Integers as Characters in Python, But when I am trying to Print it the first few values are being missing,

Here is what is happening,

S = [55, 58, 5, 13, 14, 12, 22, 20, 70, 83, 90, 69, 84, 91, 80, 91]
# Now create an empty String,
D = ""
for i in S:
    D += chr(i)

D = ', '.join(map(str, D))
print(D)

The final output is,

, ♫, ♀, ▬, ¶, F, S, Z, E, T, [, P, [

Clearly, the chr values for 55 , 58, 5 and 13 are not printed

Here is the value which are not printed,

7:♣

The chr value of 13 is perhaps a space or enter key. This is just my guess because when I am printing chr(13) , nothing shows up. But why it is removing the first three values too. ?

However, If I apply the condition,

if i == 13:
    i = i-1

The code prints all the values,

7, :, ♣, ♀, ♫, ♀, ▬, ¶, F, S, Z, E, T, [, P, [

Upvotes: 2

Views: 1372

Answers (1)

utdemir
utdemir

Reputation: 27216

Ascii 13 is carriage return(\r, enter key). Think as in type writer, it returns the cursor to start of the line, but it doesn't create a new line(\n), so when you print something, it overwrites what was in that line before. Look at this:

for i in range(10):
    print("\r%d" % i, end="")
    sleep(1)

Extra information: On Unix based systems, \n implies carriage return and new line, but on Windows systems, you should specify new line(\n) and carriage return(\r) explicitly. That's why text files created on Unix based systems looks like one long continious line on Windows.

Upvotes: 8

Related Questions