在Python中替换字符串中的名字
我正在做一个作业,需要把“Alice”替换成我的名字。这里是程序,
for i in range(10):
print(i)
print(split_alice[i]).replace_alice("Alice","Alex")
所以我只需要帮助,让程序在范围部分读取我的名字,而不是“Alice”。
谢谢。
1 个回答
0
只需要在字符串上使用 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