2024年06月03日 建站教程
如何利用python语法去除字符串最后的换行符\n
,下面web建站小编给大家简单介绍一下具体实现代码!
语法介绍:
s = s.replace('\n','').replace('\r','')
示例如下:
f = open('test.txt', 'r', encoding = 'UTF-8') labelstr = f.readlines() a = range(0,200) index = [str(i) for i in a] txt = [label.split(' ')[0].replace('\n','').replace('\r','') for label in labelstr] labeldic = dict(zip(index, txt))
删除全部换行符
str1 = "\n Starbucks has the best coffee \n" newstr = str1.strip() print(newstr)
本文链接:http://so.lmcjl.com/news/5854/