Python Forum
Print characters in a single line rather than one at a time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print characters in a single line rather than one at a time
for x in range(len(s)):
    if x % 2!=0:
This is the code I am using to print every other character in a string. However, when I run it, it prints the characters like this:
How can I get it to print them in a single line?(ex. abcd)
You can tell print not to insert a newline:
print(s[x], end="")
But my preference would be to join() the bits together before printing.

>>> s = "abcdefg"
>>> print("".join(char for index, char in enumerate(s) if index % 2 != 0))
>>> print(s[1::2])  #Or to do it directly

Possibly Related Threads…
Thread Author Replies Views Last Post
  Print to a New Line when Appending File DaveG 0 334 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Presenting multiline data into single line aaronbuhu 1 1,029 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  How to print results of asyncio websockets at the same time? codingmonster 0 1,211 Jun-04-2021, 01:48 PM
Last Post: codingmonster
  beginner text formatting single line to column jafrost 4 2,002 Apr-28-2021, 07:03 PM
Last Post: jafrost
  If match not found print last line tester_V 2 1,960 Apr-26-2021, 05:18 AM
Last Post: tester_V
  Read characters of line and return positions Gizzmo28 2 1,162 Nov-04-2020, 09:27 AM
Last Post: perfringo
  print a line break in writelines() method leodavinci1990 1 2,258 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  How to print n days back date at give time Mekala 1 1,229 Oct-10-2020, 03:35 AM
Last Post: bowlofred
  get two characters, count and print from a .txt file Pleiades 9 1,834 Oct-05-2020, 09:22 AM
Last Post: perfringo
  How to print string multiple times on new line ace19887 7 3,497 Sep-30-2020, 02:53 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020