text = "hi, hello! hello! 我有一只貓 你好!火燒了88988° 飛機好快" #Exmple string。
print(len(text))
if(len(text) > 20) 。
#text = text[:-10].
t_len = len(text) (-len(text) - 10)
text = text[:-t_len]
print(len(text))
print(text)
我嘗試了幾種方法,但我需要的是,如果len (string)> n,然后提取我并將最后的50個字符保存在另一個變數中
。uj5u.com熱心網友回復:
如果我理解正確的話,你需要像這樣的東西:
def extractor(n, string)。
output = "": 輸出 = "".
if len(string) > n。
newstring = string[-50:]
output = output newstring
return output
如果你想只回傳大于n的東西,或者你想回傳整個字串 if len(string) > 51. 那么你可以添加一個else陳述句或改變函式,但你所問的事情在上面的函式中已經解決了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/332332.html
標籤:
