[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I print string without end of line character?

Posted on 2008-06-18
3
Medium Priority
?
4,637 Views
Last Modified: 2012-06-27
I'm trying to write a script that print first 50 numbers separated by , without any spaces, end of line characters between number. How can I do that?
I tried to use:
print ",",
however it generates extra space between words
#!/usr/bin/python
 
for i in range(1,51):
	if i > 1:
		print ","
	print "%d" % i

Open in new window

0
Comment
Question by:pmnox
3 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 1000 total points
ID: 21816542
Try:
#!/usr/bin/python
 
import sys
for i in range(1,51):
     if i>1:
             sys.stdout.write(',')
     print i,

Open in new window

0
 
LVL 17

Expert Comment

by:ramrom
ID: 21816578

#!/usr/bin/python
 
print ",".join(str(i) for i in range(50))

Open in new window

0
 

Author Closing Comment

by:pmnox
ID: 31468524
thx. I didn't know that I can use sys.stdout.write function instread of print
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question