![]() |
|
[Tut] 5 Pythonic Ways to Print a List without Brackets - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] 5 Pythonic Ways to Print a List without Brackets (/thread-99456.html) |
[Tut] 5 Pythonic Ways to Print a List without Brackets - xSicKxBot - 05-25-2022 5 Pythonic Ways to Print a List without Brackets 5/5 – (1 vote) Problem Formulation and Solution Overview In this article, you’ll learn how to print the contents of a List without surrounding brackets in Python. To make it more fun, we have the following running scenario: You are a student and need to memorize the first 10 elements in the Periodic Table. This data is currently saved in a List format. However, you would prefer it to display without brackets to remove any distractions. Question: How would we write Python code to print a List without brackets?We can accomplish this task by one of the following options:
Method 1: Use print() and join() This method uses Code: join()Code: ListCode: print()Code: join()Code: 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 Code: periodic_elsNext, Code: join()Code: periodic_elsCode: printCode: ' / '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 Code: printCode: ','Code: 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 (Code: ' 'Code: ''![]() Method 2: Use print(*, sep) This method uses Code: print()Code: *periodic_elsCode: ' 'Code: periodic_els = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne']
print(*periodic_els, sep=' ') Note: Placing an asterisk (Code: *This code declares a List of the first 10 elements in the Periodic Table and saves them to Code: periodic_elsInside the Code: print()Code: *periodic_elsNext, the Code: sepCode: print()Code: ' '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 Code: slicingCode: 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 Code: periodic_elsNext, the Code: print()Code: [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 Code: periodic_elsCode: join()Code: map()Code: 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 Code: periodic_elsNext, Code: periodic_elsCode: map()(‘, ‘) 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 ( Code: '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 Code: replace("'", "")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! |