Home > Backend Development > Python Tutorial > How to Print a Python List Without Braces and on a Single Line?

How to Print a Python List Without Braces and on a Single Line?

Linda Hamilton
Release: 2024-12-01 12:22:10
Original
1047 people have browsed it

How to Print a Python List Without Braces and on a Single Line?

Eliminating Braces and Printing Lists in a Single Row

Given a list in Python, such as:

names = ["Sam", "Peter", "James", "Julian", "Ann"]
Copy after login

Ordinarily, printing this list would produce the output:

["Sam", "Peter", "James", "Julian", "Ann"]
Copy after login

However, to format the list without brackets and in a single row, follow these steps:

  1. Convert the list to a string: Use the join() method with a separator of ', '. This will concatenate the elements of the list with commas and spaces.
string_names = ', '.join(names)
Copy after login
  1. Print the string:
print(string_names)
Copy after login

This will produce the desired output:

Sam, Peter, James, Julian, Ann
Copy after login

The above is the detailed content of How to Print a Python List Without Braces and on a Single Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template