-3

I a have a list generated from some input eg:

text = input("Please enter the text you would like to encrypt: ")
textlist = text.split

If I want to convert all the A's in the list to B's, how can I do this?

Eg Input: Hello my name is ana

Output: Hello my nbme is bnb

dot.Py
  • 5,007
  • 5
  • 31
  • 52
  • 1
    `'Hello my name is ana Output'.replace('a','b').replace('A','B')` or use `str.translate` – Chris_Rands Jan 19 '17 at 16:55
  • 6
    Possible duplicate of [Replacing instances of a character in a string](http://stackoverflow.com/questions/12723751/replacing-instances-of-a-character-in-a-string) – Yevhen Kuzmovych Jan 19 '17 at 16:57
  • Possible duplicate of [New to Python, replacing characters in a string](http://stackoverflow.com/questions/35810449/new-to-python-replacing-characters-in-a-string) – dot.Py Jan 19 '17 at 16:58

1 Answers1

0

text.split just returns an instance of a built-in python method.

>>> text.split
<built-in method split of str object at 0x10c0b36c0>

If you want to use the split method, you need to do text.split() (with the parentheses). in any case, this method does not do what you are looking for - it will return a list with one element:

>>> text.split()
['hello my name is ana']

To convert the string to a list of characters, use list(text):

>>> list(text)
['h', 'e', 'l', 'l', 'o', ' ', 'm', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'a', 'n', 'a'] 

However, in any case, simply replacing characters in the string is much easier than that, and should not require converting the string to a list.

>>> text.replace('a', 'b')
'hello my nbme is bnb'
danyamachine
  • 1,848
  • 1
  • 18
  • 21