Python 字符串和变量赢得';t在同一行上连接
我无法使用下面的代码在同一行上打印一个单词的序列号和定义。任何建议都将不胜感激。我已经阅读了之前关于这个问题的问答,但没有运气Python 字符串和变量赢得';t在同一行上连接,python,python-3.x,concatenation,Python,Python 3.x,Concatenation,我无法使用下面的代码在同一行上打印一个单词的序列号和定义。任何建议都将不胜感激。我已经阅读了之前关于这个问题的问答,但没有运气 from bs4 import BeautifulSoup import requests loops = "" while loops == "": url = "http://dictionary.reference.com/browse/" c = 1 word = input("Enter word (0 to quit): "
from bs4 import BeautifulSoup
import requests
loops = ""
while loops == "":
url = "http://dictionary.reference.com/browse/"
c = 1
word = input("Enter word (0 to quit): ")
if word == "0":
break
data = requests.get(url + word)
soup = BeautifulSoup(data.text, "html.parser")
data1 = soup.find_all("div",{"class":"def-content"})
print("The meaning/s of " + word + " is/are:")
for i in data1:
if i.string != None:
print(c, i.string) # Trying to print serial number and definition on same line
c = c + 1
试试这个:
print(str(c)+ " " + i.string.strip('\n'))
错误到底是什么?或者你的输出与你期望的有什么不同?没有错误,我只是希望我的输出的两个组成部分,(I)序列号和(ii)单词含义出现在同一行,但我得到“1”,然后下一行是单词的含义,第三行是“2”,第四行是第二个含义,依此类推。谢谢。因此您的
i.string
在开头包含\n
。多大的一件事啊。如果不符合您的需要,请尝试使用strip\u string
,您可能只需要.strip()
不,我的意思是,正如我所示-str.strip
不带参数将删除所有空白字符,包括'\n'
,默认情况下。他是想删除所有前导和尾随的空格字符还是只删除换行符?一般来说,最简单的方法是删除所有空格字符,然后使用str.join
或str.format
返回您需要的内容(如果有)!谢谢你,小阿科尼斯。只是想删除新的线路。