我正在創建一個字符串,它必須完全是140個字符。我必須使用帶有break語句的循環,並且字符串項來自給定列表。Python:如果符合字符限制,如何打破循環?
最後,我設法達到了147個字符。有沒有辦法截斷字符串?
headlines = ["Local Bear Eaten by Man",
"Legislature Announces New Laws",
"Peasant Discovers Violence Inherent in System",
"Cat Rescues Fireman Stuck in Tree",
"Brave Knight Runs Away",
"Papperbok Review: Totally Triffic"]
news_ticker = ""
for headline in headlines:
if len(str(news_ticker)) >= 140:
break
else:
news_ticker = list(news_ticker)
news_ticker.append(headline)
print(news_ticker)
print(len(str(news_ticker)))