Solved

How can I print string without end of line character?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now