簡體   English   中英

如何在 python 的列表中剪切字符串

[英]How to cut strings inside a list in python

例如,列表將包含

list1 = ["mathematics", "sciences", "historical"]

我只需要字符串的中間部分

output_list = ["thema", "ience", "stori"]

Python 列表理解。 請閱讀它,它很有用。

此外,python 切片和索引。 你可以在這里閱讀更多

對於您發布的問題:

output_list = [word[2:7] for word in list1]

print(output_list) 

您可以為此使用字符串切片:

x = "abcde"
print(x[1:4])  # prints elements [1, 4), i.e., "bcd"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM