Problem Formulation and Solution Overview
Method 1: Use print() and join()
This method uses join()
to access each element of the List
passed. Then print()
lets join()
know which separator to concatenate (append) to each element. The result is a String.
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(' / '.join(periodic_els))
This code declares a List of the first 10 element names of the Periodic Table and saves them to periodic_els
.
Next, join()
passes periodic_els
as an argument and accesses each element, adding the appropriate separator character(s) as indicated in the print
statement (' / '
).
Finally, the output is sent to the terminal as a String data type.
Output
H / He / Li / Be / B / C / N / O / F / Ne |
If we modified the print
statement to have a comma (','
) as a separator, the output would be as follows:
elements = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(','.join(elements))
Output
H,He,Li,Be,B,C,N,O,F,Ne |
π‘Β Note: We recommend trying different separating characters, including a space (' '
) and an empty space (''
) to see the output.
Method 2: Use print(*, sep)
This method uses print()
passing *periodic_els
, and a separator character (' '
) as arguments to the same.
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(*periodic_els, sep=' ')
π‘ Note: Placing an asterisk (*
) before the variable lets Python know to unpack/extract the variable, in this case, a List.
This code declares a List of the first 10 elements in the Periodic Table and saves them to periodic_els
.
Inside the print()
statement *periodic_els
is passed as the first argument. This tells Python to unpack the List as indicated above.
Next, the sep
argument is passed to print()
. This will concatenate (append) character(s) between each element, in this case, a space (' '
).
Finally, the output is sent to the terminal as a String data type.
Output
H He Li Be B C N O F Ne |
Method 3: Use Slicing
This method uses slicing
to remove the brackets. However, the single quote characters surrounding each element will remain.
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(str(periodic_els)[1:-1])
This code declares a List of the first 10 elements in the Periodic Table and saves them to periodic_els
.
Next, the print()
statement converts the List to a string and then uses slicing to extract the output as indicated by [1:-1]
.
Finally, the output is sent to the terminal as a String data type.
Output
‘H’, ‘He’, ‘Li’, ‘Be’, ‘B’, ‘C’, ‘N’, ‘O’, ‘F’, ‘Ne’ |
Method 4: Use join() and map()
If periodic_els
contained integers instead of strings, it would need to be converted to a String data type first. Then, join()
and map()
are used to output the contents without brackets.
periodic_els = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(', '.join(map(str, periodic_els)))
This code declares a List of the first 10 elements in the Periodic Table and saves them to periodic_els
.
Next, periodic_els
is converted to a String, and the iterable map()
object is accessed. Then, each element is evaluated, and a separator character
(‘, ‘) is placed between each element and concatenated.
Finally, the output is sent to the terminal as a String data type.
Output
1, 2, 3, 4, 5, 6, 7, 8, 9,10 |
Bonus: Strip quote characters from Method 3
This section expands on Method 3, where the resultant output contained quote characters ('
) surrounding each element. This can easily be removed by running the following code.
periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'] print(str(periodic_els)[1:-1].replace("'", ""))
The only difference between this code from Method 3 and the code above is that replace("'", "")
is appended to the end.
Output
H, He, Li, Be, B, C, N, O, F, Ne |
Summary
These four (4) methods of how to print a List without Brackets should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!