0

課題に取り組んでおり、アリスを自分の名前に置き換える必要があります。ここにプログラムがあります、

for i in range(10):
    print(i)
    print(split_alice[i]).replace_alice("Alice","Alex")

iしたがって、アリスではなく私の名前でプログラムが範囲内の部分を読み取れるようにするための助けが必要です。

ありがとう。

4

1 に答える 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
于 2013-03-03T16:21:44.477 に答える