我正在处理一项任务,我需要用我的名字替换 Alice。这是程序,
for i in range(10):
print(i)
print(split_alice[i]).replace_alice("Alice","Alex")
所以我只需要帮助让程序i
用我的名字而不是 Alice 来读取范围内的部分。
谢谢。
只需replace()
在字符串上使用方法..
就像这样
>>> mystr = 'My name is Alice'
>>> mystr.replace('Alice','Alex')
'My name is Alex'
此外,如果您对任何事情有任何疑问,您应该使用help()
.
就像你的情况一样:
>>> help(type(mystr))
Help on class str in module __builtin__:
class str(basestring)
| str(object) -> string
...
...
| 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.
|
...
...
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T