永发信息网

Python 怎样用变量替换字符串?

答案:2  悬赏:40  手机版
解决时间 2021-03-08 11:49
  • 提问者网友:孤山下
  • 2021-03-08 01:14
num1 = 2
Xpath = '//*[@id="custMtTranList"]/tbody/tr[1]/td[11]'
print Xpath.replace(Xpath[35:36], num1)
运行结果:
Traceback (most recent call last):
File "", line 1, in
print Xpath.replace(Xpath[35:36],num1)
TypeError: expected a string or other character buffer object
最佳答案
  • 五星知识达人网友:底特律间谍
  • 2021-03-08 01:57
很简单

只要把
Xpath.replace(Xpath[35:36], num1)改成

Xpath.replace(Xpath[35:36], str(num1))以下是replace方法的详细说明
 replace(...)
     S.replace(old, new[, count]) -> string

     Return a copy of string S with all occurrences of substring
     old replaced by new.  If the optional argument count is
     given, only the first count occurrences are replaced.参数只能是str类型
全部回答
  • 1楼网友:底特律间谍
  • 2021-03-08 02:20
1. 使用连接符: + world = "world" print "hello " + world + " ! "2. 使用占位符来内插 world = "world" print "hello %s !" % world3. 使用函数 li = ['my','name','is','bob'] mystr = ' '.join(li) print mystr 上面的语句中字符串是作为参数传入的,可以直接用变量替换: begin_date = '2012-04-06 00:00:00' end_date = '2012-04-06 23:59:59' select * from usb where time between to_date(begin_date,'yyyy-mm-dd hh24:mi:ss') and to_date(end_date,'yyyy-mm-dd hh24:mi:ss')
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯