2

Say you have the following code:

bicycles = ['Trek','Cannondale','Redline','Secialized']
print(bicycles[0],bicycles[1],bicycles[2],bicycles[3])

This would print out:

Trek Cannondale Redline Specialized

I have two questions. First, Is there a way to make the print string more organized so that you don't have to type out bicycles multiple times? I know that if you were to just do:

print(bicycles)

It would print the brackets also, which I'm trying to avoid.

Second question, how would I insert commas to display within the list when its printed?

This is how I would like the outcome:

Trek, Cannondale, Redline, Specialized.

I know that I could just do

print("Trek, Cannondale, Redline, Specialized.")

But using a list, is there anyway to make it more organzed? Or would printing the sentence out be the smartest way of doing it?

sparx
  • 51
  • 4
  • you can use "for" loop to iterate through each list element and print it out. "for bicycle in bicycles: print (bicycle)" will do the job – Volodymyr Vyshnevskyi Jul 15 '19 at 15:34
  • 2
    Possible duplicate of [Print list without brackets in a single row](https://stackoverflow.com/questions/11178061/print-list-without-brackets-in-a-single-row) – Mark Jul 15 '19 at 15:36

2 Answers2

7

use .join() method:

The method join() returns a string in which the string elements of sequence have been joined by str separator.

syntax: str.join(sequence)

bicycles = ['Trek','Cannondale','Redline','Secialized']
print (' '.join(bicycles))

output:

Trek Cannondale Redline Secialized

Example: change separotor into ', ':

print (', '.join(bicycles))

output:

Trek, Cannondale, Redline, Secialized

For python 3. you can also use unpacking:

We can use * to unpack the list so that all elements of it can be passed as different parameters.

We use operator *

bicycles = ['Trek','Cannondale','Redline','Secialized']
print (*bicycles)

output:

Trek Cannondale Redline Secialized

NOTE: It's using ' ' as a default separator, or specify one, eg:

print(*bicycles, sep=', ')

Output:

Trek, Cannondale, Redline, Secialized

It will also work if the elements in the list are different types (without having to explicitly cast to string)

eg, if bicycles was ['test', 1, 'two', 3.5, 'something else']

bicycles = ['test', 1, 'two', 3.5, 'something else']
print(*bicycles, sep=', ')

output:

test, 1, two, 3.5, something else
ncica
  • 7,015
  • 1
  • 15
  • 37
  • 1
    Since the OP says they're using Python 3.x, then another option is `print(*bicycles)` which'll use `' '` as a default separator, or specify one, eg: `print(*bicycles, sep=', ')`... that'll also work if it's mixed types without having to explicitly cast to string, eg, if `bicycles` was `['test', 1, 'two', 3.5, 'something else']`... – Jon Clements Jul 15 '19 at 15:41
  • How would you include a period at the end? – sparx Jul 15 '19 at 15:51
  • I hope this isn't taken as a snobby comment, but it would probably also be worth learning how to do this manually as well. `for y in range(len(bicycles)): if y==len(bicycles)-1: end="" else: end="," print(bicycles[y],end=end)` – Jack Strosahl Jul 15 '19 at 15:52
  • Thank you for nice advice, if you do not mind, I updated my post :) @JackStrosahl – ncica Jul 15 '19 at 15:57
  • @sparx like so: `print(*bicycles, sep=', ', end='.')` – Tomerikoo Jul 15 '19 at 16:16
1

You can use join:

' '.join(bicycles)

', '.join(bicycles)
Carsten
  • 2,765
  • 1
  • 13
  • 28