Solved

How can I print string without end of line character?

Posted on 2008-06-18
3
4,602 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 250 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

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

830 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