这真让我抓狂。我有一个这样的字符串:
Sheridan School III
在这样的列表中:
Sheridan School III
Lake Bluff Elementary II
Barrington IV
我想把尾随部分变成一个变量homeseries
并将之前的部分变为变量homeclub
当我拆分字符串时,我可以得到系列,没问题,如下所示。
但是我不能在.replace
字符串中使用该系列,我只想用它来摆脱原始字符串的最后一部分。如何在 .replace 中使用变量?Python 抛出一个错误,我确信有一种简单的方法可以将它作为变量引用。
homeclubfull = "Sheridan School III"
homeclublist = homeclubfull.split(" ")
homeseries = homeclublist[-1:] # this should return "III"
homeclub=homeclubfull.replace(homeseries,'')
print homeclub, home series
抛出的错误:
import.py:51: TypeError: expected a character buffer object