So I have a string like so:
pattern = "AAaa$$##"
I want to parse it into a list so it outputs letter by letter, but I can't do pattern.split("")
cause it gives an error. Like this pattern.split()
wont work either, it will just give me back the same string but in a list instead.
I need it to give me a list like this: ["A", "A", "a", "a", "$", "$", "#", "#"]
Thanks in advance.
Just do
list(pattern)
It creates a list. Save it to any variable and use it. See the Python Docs entry.
For instance, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]
Although, you can just iterate over the strings like so
>>> for elem in testString:
# Do Something
Test-
>>> pattern = "AAaa$$##"
>>> list(pattern)
['A', 'A', 'a', 'a', '$', '$', '#', '#']
>>> pattern = 'AAaa$$##'
>>> list(pattern)
['A', 'A', 'a', 'a', '$', '$', '#', '#']
But if you just want to outputs the string letter by letter, there is no need to parse it into a list. The string itself is a sequence.
>>> for letter in pattern:
... print letter
...
A
A
a
a
$
$
#
#
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.