0

I want to split the ascii_letters* intoa list (in the string module) and it doesn't have any repeated characters. I tried to put the split marker as '' but that didn't work; I got an ValueError: empty separator message. Is there a string manipulator other than split() which I can use? I might be able to put spaces in, but that may become tedious and might take up a lot of code space.

import string
letters = string.ascii_letters
print(letters.split('')) 

*The ascii_letters is a string that contains 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ'

Iain
  • 3
  • 5
  • Do you want to separate a string into characters? – pecey Jul 03 '20 at 14:37
  • 1
    A string is already a sequence of characters. In most cases there is no need to split it. If you really need a list just do a `list(string.ascii_letters)`. – Klaus D. Jul 03 '20 at 14:37
  • You cannot split at empty separator. It does not make sense. – Pramote Kuacharoen Jul 03 '20 at 14:40
  • @PramoteKuacharoen in some programming languages splitting a string on an empty delimiter *does* split the string into a list of characters. The idea makes sense (since it would make `.split('')` dual to `''.join()`) -- but it isn't python. – John Coleman Jul 03 '20 at 14:49

2 Answers2

0

list(letters)

might be what you are looking for.

hetepeperfan
  • 4,292
  • 1
  • 29
  • 47
0

You can use a regex to split a string using split() of the re module.

re.split(r'.', str)

To split at every character.

Or simply use list(str) to get the list of characters as suggested by @Klaus D.

genius42
  • 243
  • 1
  • 6