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
  No new line from print in a browser Lou 13 1,067 Dec-01-2025, 06:39 PM
Last Post: noisefloor
  problem with spliting line in print akbarza 3 1,978 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print names in x-axis of a time-series values hobbyist 4 3,203 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 4,242 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 2,272 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Presenting multiline data into single line aaronbuhu 1 3,052 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  How to print results of asyncio websockets at the same time? codingmonster 0 2,640 Jun-04-2021, 01:48 PM
Last Post: codingmonster
  beginner text formatting single line to column jafrost 4 5,292 Apr-28-2021, 07:03 PM
Last Post: jafrost
  If match not found print last line tester_V 2 4,253 Apr-26-2021, 05:18 AM
Last Post: tester_V
  Read characters of line and return positions Gizzmo28 2 3,250 Nov-04-2020, 09:27 AM
Last Post: perfringo

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.