0

So i wrote a code that takes a string and print out the ascii code but i have this problem that i'm printing out the ascii for every letter in a for loop and in the end i want it to be a string of a number.

This is the code:

getname='test'
for letter in getname:
    print ord(letter)

And the output is:

116
101
115
116

How can i take the for loop output and make it a string? in the end i want it to be like this:

116101115116

Thanks.

2
  • ''.join([str(ord(letter)) for letter in getname]) Commented Jul 21, 2016 at 14:04
  • Maybe need padding front a 0 for all fixed length. Commented Jul 21, 2016 at 14:08

2 Answers 2

2

You can do a one line statement like this

>>> "".join(str(ord(x)) for x in getname)
'116101115116'
Sign up to request clarification or add additional context in comments.

Comments

1

You want to create a string and append to it, like this:

getname = 'test'
result = ''
for letter in getname:
    result += ord(letter)
print result

Output:

116101115116

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.