【问题标题】:adding to a string without commas添加到不带逗号的字符串
【发布时间】:2012-01-09 01:33:15
【问题描述】:

我想在不带任何逗号的字符串中添加内容。

我有一个字符串 URL:http://gdata.youtube.com/feeds/api/standardfeeds/feed_str?max-results=max_result&time=

feed_str = top_rated
max_result = 5
time_str = today
web_str = "http://gdata.youtube.com/feeds/api/standardfeeds/",feed_str, "?max-results=" ,max_result,"&time=" + time_str

这样打印出来的

('http://gdata.youtube.com/feeds/api/standardfeeds/', 'top_rated', '?max-results=', '5', '&time=today')

我希望它像这样打印:

('http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max-results=5'&time=today')

这只是一个字符串。有什么帮助吗?!

【问题讨论】:

  • 线索已经在你的代码中了。看看你的输出。在字符串构造中使用变量time_str 的方式有什么不同?

标签: python


【解决方案1】:

String concatenation 是您正在寻找的内容,SO 有很多关于此主题的主题(String concatenation vs. string substitution in PythonConcatenation Operator + or ,,...)

【讨论】:

    【解决方案2】:

    只需删除web_str 上的逗号并将信号'+' 放在它们的位置。

    【讨论】:

    • 是的,非常好。然而,大多数人认为最好为homework 问题提供提示,而不是直接回答它们,因为这给提问者提供了学习的机会,而不仅仅是完成作业。请参阅How to ask and answer homework questions 了解更多信息。
    • 对不起,我的错。没有看到这是一个家庭作业问题。下次我会多注意的。谢谢你的建议。
    【解决方案3】:

    这里有一个提示;您正在寻找concatenate 字符串的方法。

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多