字符串和变量不会在同一行连接

时间:2015-09-13 22:21:12

标签: python python-3.x concatenation

我无法使用以下代码在同一行上打印单词的序列号和定义。任何建议将不胜感激。我已经阅读了之前问过的Q& A,但没有运气。

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

1 个答案:

答案 0 :(得分:3)

试试这个:

        print(str(c)+ " " + i.string.strip('\n'))