I am iterating through a string in python and I want to check each character to see if it equals "
. How do I go about doing this?
Asked
Active
Viewed 2.7k times
1 Answers
12
Like this:
for c in theString:
if c == '"':
print 'Aha!'
You can also directly get the index of the first quote like so:
theString.index('"')

Cameron
- 96,106
- 25
- 196
- 225
-
2Note that this is probably best replaced with `if '"' in some_string: ...` (unless you are already looping through the string for other purposes or some such). – Gareth Latty Feb 25 '13 at 03:11
-
@Lattyware: Absolutely, if the goal is merely to check if there is a quote at all. – Cameron Feb 25 '13 at 03:11
-
Naturally, it depends on the goal. – Gareth Latty Feb 25 '13 at 03:12
-
Note: there is a modest performance advantage using `if c is '"':` – Octipi Feb 25 '13 at 03:45
-
1@Eric: Good point, [but be careful](http://stackoverflow.com/a/1392927/21475) as that's an implementation detail, not a guarantee of the language. – Cameron Feb 25 '13 at 03:55