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
Linda HamiltonOriginal
2024-12-01 12:22:101023browse

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"]

Ordinarily, printing this list would produce the output:

["Sam", "Peter", "James", "Julian", "Ann"]

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)
  1. Print the string:
print(string_names)

This will produce the desired output:

Sam, Peter, James, Julian, Ann

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:
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