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
#1
 
for x in range(len(s)):
    if x % 2!=0:
        print(s[x])
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:
a
b
c
d
How can I get it to print them in a single line?(ex. abcd)
Reply
#2
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))
bdf
>>> print(s[1::2])  #Or to do it directly
bdf
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 335 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print names in x-axis of a time-series values hobbyist 4 1,176 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,488 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 1,189 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Presenting multiline data into single line aaronbuhu 1 1,769 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  How to print results of asyncio websockets at the same time? codingmonster 0 1,744 Jun-04-2021, 01:48 PM
Last Post: codingmonster
  beginner text formatting single line to column jafrost 4 3,160 Apr-28-2021, 07:03 PM
Last Post: jafrost
  If match not found print last line tester_V 2 2,845 Apr-26-2021, 05:18 AM
Last Post: tester_V
  Read characters of line and return positions Gizzmo28 2 1,975 Nov-04-2020, 09:27 AM
Last Post: perfringo
  print a line break in writelines() method leodavinci1990 1 6,358 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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