How to print two numbers separated by space in python

I have a list L of elements, say natural numbers. I want to print them in one line with a single space as a separator. But I don't want a space after the last element of the list [or before the first].

In Python 2, this can easily be done with the following code. The implementation of the print statement [mysteriously, I must confess] avoids to print an extra space before the newline.

L = [1, 2, 3, 4, 5]
for x in L:
    print x,
print

However, in Python 3 it seems that the [supposedly] equivalent code using the print function produces a space after the last number:

L = [1, 2, 3, 4, 5]
for x in L:
    print[x, end=" "]
print[]

Of course there are easy answers to my question. I know I can use string concatenation:

L = [1, 2, 3, 4, 5]
print[" ".join[str[x] for x in L]]

This is a quite good solution, but compared to the Python 2 code I find it counter-intuitive and definitely slower. Also, I know I can choose whether to print a space or not myself, like:

L = [1, 2, 3, 4, 5]
for i, x in enumerate[L]:
    print[" " if i>0 else "", x, sep="", end=""]
print[]

but again this is worse than what I had in Python 2.

So, my question is, am I missing something in Python 3? Is the behavior I'm looking for supported by the print function?

Last update on August 19 2022 21:51:47 [UTC/GMT +8 hours]

Python List: Exercise - 62 with Solution

Write a Python program to print a list of space-separated elements.

Sample Solution:-

Python Code:

num = [1, 2, 3, 4, 5]
print[*num]

Sample Output:

1 2 3 4 5

Flowchart:


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code [and comments] through Disqus.

Previous: Write a Python program to create a list of empty dictionaries.
Next: Write a Python program to insert a given string at the beginning of all items in a list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Python: Tips of the Day

Checking whether all elements in the sequence are Truthful:

>>> all[a % 2==0 for a in range[0,10,2]]
True

  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation

To convert a space-separated string to a list in Python, call the str.split[] method:

sentence = "This is a test"

words_list = sentence.split[]

print[words_list]

Output:

['This', 'is', 'a', 'test']

This works because the str.split[] splits the string by blank spaces by default.

Let’s then take a look at how to split a string of integers into a list of integers.

To convert a space-separated string of integers to a list in Python:

  1. Split the string on empty spaces.
  2. Convert each element to an integer.
  3. Add each integer to a list.

You can do this with a for loop:

numbers_str = "1 2 3 4 5"

numbers_list = []

for num_str in numbers_str.split[]:
    num_int = int[num_str]
    numbers_list.append[num_int]

print[numbers_list]

Output:

[1, 2, 3, 4, 5]

To make the expression way shorter, you can use a list comprehension:

numbers_str = "1 2 3 4 5"

numbers_list = [int[num] for num in numbers_str.split[]]

print[numbers_list]

Output:

[1, 2, 3, 4, 5]

Conclusion

Thanks for reading. I hope you found the answer you were looking for.

Happy coding!

Further Reading

Python Tricks

Python List Comprehensions

How do you put a space between two numbers in Python?

Use print[] to add a space between two variables Call print[variable1, variable2] to print variable1 and variable2 with a space between them.

How do you take space

For taking a variable number of space-separated inputs we usually assign them to a list but you could also use set or tuple according to your requirement. If you want sets or tuples then use tuple[] or set[] instead of list[] .

How do you print a space in a statement in Python?

Spacing in Python language is quite simple than other programming language. In C languages, to print 10 spaces a loop is used while in python no loop is used to print number of spaces.

How do you print the integer list of integers separated by space in Python?

You can print list integers separated by space using join and map functions..
n = int[input[“Enter the value of n:"]].
for i in range[n]:.
print[i, end="“].
Output:.
Enter the value of n: 5..
12345..

Chủ Đề