Home > Enterprise >  How can I print in one single line after using a for loop
How can I print in one single line after using a for loop

Time:07-19

I need your help. So, I have to separate the words of a string, then I have to sort the letters of the words alphabetically and print them out in one line.

words = "apple pumpkin log river fox pond"
words = words.split()

for i in words:
  print("".join(sorted(i)))

CodePudding user response:

for i in sorted(words): print("".join(i))

could also do something like initialize an empty string, then add each word with a empty string to space them out

CodePudding user response:

You can use use:

words = "apple pumpkin log river fox pond"
words = words.split()

for i in words:
    print("".join(sorted(i)),end= " ")
print("")

The "end" string will be printed after the main string. The default value for it is "\n".

  • Related