在 Python 中打印没有逗号和括号的列表

目录

Print a list without the commas and brackets in Python

  1. 在 Python 中打印没有逗号和括号的列表
  2. 使用 sep 打印不带逗号和括号的列表
  3. 在 Python 中打印不带括号的列表
  4. 使用 sep 打印不带括号的列表

在 Python 中打印不带逗号和括号的列表

要打印不带逗号和括号的列表:

  1. 使用该str.join()方法将列表连接成一个字符串。
  2. 如果列表包含数字,将它们转换为字符串。
  3. 使用print()函数打印字符串。
主程序
list_of_strings = ['bobby', 'hadz', '.com'] # ✅ Print list of strings without commas, brackets and quotes result = ''.join(list_of_strings) print(result) # 👉️ bobbyhadz.com # --------------------------------------------- # ✅ Print list of integers without commas, brackets and quotes list_of_integers = [7, 21, 44] result = ''.join(str(item) for item in list_of_integers) print(result) # 👉️ 72144 # --------------------------------------------- list_of_strings = ['bobby', 'hadz', 'com'] # ✅ Print list of strings without brackets result = ', '.join(list_of_strings) print(result) # 👉️ bobby, hadz, com # --------------------------------------------- # ✅ Print list of numbers without brackets list_of_numbers = [11, 33, 55] result = ', '.join(str(item) for item in list_of_numbers) print(result) # 👉️ 11, 33, 55

我们使用该str.join()方法打印没有逗号、括号和引号的列表。

str.join方法将一个可迭代对象作为参数并返回一个字符串,该字符串是可迭代对象中字符串的串联。

请注意,TypeError如果可迭代对象中有任何非字符串值,该方法将引发 a。

如果您的列表包含数字或其他类型,请在调用之前将所有值转换为字符串join()

主程序
list_of_integers = [7, 21, 44] result = ''.join(str(item) for item in list_of_integers) print(result) # 👉️ 72144

我们使用生成器表达式来遍历列表。

生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。

在每次迭代中,我们使用str()该类将数字转换为字符串。

调用该join()方法的字符串用作元素之间的分隔符。

主程序
list_of_strings = ['bobby', 'hadz', '.com'] result = ' '.join(list_of_strings) print(result) # 👉️ bobby hadz .com # --------------------------------------------- list_of_integers = [7, 21, 44] result = ' '.join(str(item) for item in list_of_integers) print(result) # 👉️ 7 21 44

如果您不需要分隔符而只想将列表的元素连接到一个字符串中,请在join()空字符串上调用该方法。

您还可以使用该map()函数在调用之前将列表中的所有项目转换为字符串join()

主程序
list_of_integers = [7, 21, 44] result = ''.join(map(str, list_of_integers)) print(result) # 👉️ 72144

map()函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项目调用该函数。

或者,您可以在函数sep调用中使用参数print()

使用 sep #打印不带逗号和括号的列表

使用sep参数打印不带逗号和方括号的列表,例如
print(*my_list, sep=''). 列表中的项目将在print()函数调用中解包,并且打印时不带逗号、括号和引号。

主程序
list_of_strings = ['bobby', 'hadz', '.com'] print(*list_of_strings, sep='') # 👉️ bobbyhadz.com # --------------------------------------------- list_of_integers = [7, 21, 44] print(*list_of_integers, sep='') # 👉️ 72144
请注意,我们在对 的调用中使用了可迭代解包*运算符来解包列表项print()

*迭代解包运算符
使我们能够在函数调用、推导式和生成器表达式中解包可迭代对象。

sep参数是我们传递给 的参数之间的分隔print()

主程序
list_of_strings = ['bobby', 'hadz', '.com'] print(*list_of_strings, sep='') # 👉️ bobbyhadz.com print(*list_of_strings, sep=' ') # 👉️ bobby hadz .com print(*list_of_strings, sep='-') # 👉️ bobby-hadz-.com

默认情况下,sep参数设置为空格。

在 Python 中打印不带括号的列表

要打印不带括号的列表:

  1. 使用该str.join()方法将列表连接成一个字符串。
  2. 如果列表包含数字,将它们转换为字符串。
  3. 使用print()函数打印字符串。
主程序
list_of_strings = ['bobby', 'hadz', 'com'] # ✅ Print list of strings without brackets result = ', '.join(list_of_strings) print(result) # 👉️ bobby, hadz, com # --------------------------------------------- # ✅ Print list of numbers without brackets list_of_numbers = [11, 33, 55] result = ', '.join(str(item) for item in list_of_numbers) print(result) # 👉️ 11, 33, 55

我们使用该str.join()方法打印没有方括号的列表。

str.join方法将一个可迭代对象作为参数并返回一个字符串,该字符串是可迭代对象中字符串的串联。

请注意,TypeError如果可迭代对象中有任何非字符串值,该方法将引发 a。

如果您的列表包含数字或其他类型,请在调用之前将所有值转换为字符串join()

主程序
list_of_numbers = [11, 33, 55] result = ', '.join(str(item) for item in list_of_numbers) print(result) # 👉️ 11, 33, 55

我们使用生成器表达式来遍历列表。

生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。

在每次迭代中,我们使用str()该类将数字转换为字符串。

调用该join()方法的字符串用作元素之间的分隔符。

主程序
list_of_strings = ['bobby', 'hadz', 'com'] result = ' '.join(list_of_strings) print(result) # 👉️ bobby hadz com

如果您不需要分隔符而只想将列表的元素连接到一个字符串中,请在join()空字符串上调用该方法。

主程序
list_of_numbers = [11, 33, 55] result = ''.join(str(item) for item in list_of_numbers) print(result) # 👉️ 113355

您还可以使用该map()函数在调用之前将列表中的所有项目转换为字符串join()

主程序
list_of_numbers = [11, 33, 55] result = ', '.join(map(str, list_of_numbers)) print(result) # 👉️ 11, 33, 55

map()函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项目调用该函数。

或者,您可以在函数sep调用中使用参数print()

使用 sep #打印不带括号的列表

使用sep参数打印不带括号的列表,例如
print(*my_list, sep=', '). 列表中的项目将在对的调用中解包,print()并将使用逗号分隔符打印,不带方括号。

主程序
list_of_strings = ['bobby', 'hadz', 'com'] # 👇️ bobby, hadz, com print(*list_of_strings, sep=', ') # ---------------------------------------------- list_of_numbers = [11, 33, 55] # 👇️ 11, 33, 55 print(*list_of_numbers, sep=', ')
请注意,我们在对 的调用中使用了可迭代解包*运算符来解包列表项print()

*迭代解包运算符
使我们能够在函数调用、推导式和生成器表达式中解包可迭代对象。

sep参数是我们传递给 的参数之间的分隔print()

主程序
print('bobby', 'hadz', sep='') # 👉️ bobbyhadz print('bobby', 'hadz') # 👉️ bobby hadz

默认情况下,sep参数设置为空格。

一种不太灵活的方法是将列表转换为字符串并使用字符串切片。

主程序
list_of_strings = ['bobby', 'hadz', 'com'] # 👇️ 'bobby', 'hadz', 'com' print(str(list_of_strings)[1:-1]) # --------------------------------------------- list_of_numbers = [11, 33, 55] # 👇️ 11, 33, 55 print(str(list_of_numbers)[1:-1])

我们使用str()该类将列表转换为字符串,并使用字符串切片来排除方括号。

字符串切片的语法是my_str[start:stop:step].

索引是包含的start,而stop索引是排他的(最多,但不包括)。

Python 索引是从零开始的,因此字符串中的第一个字符的索引为0,最后一个字符的索引为-1or len(my_str) - 1

我们使用一个start索引1排除左方括号,并使用一个
stop索引-1排除右方括号。