1

I was wondering if there is a way in Python 3.5 to check if a string contains a certain symbol. Also I'd like to know if there is a way to check the amount the string contains. For example, if I want to check how many times the character '$' appears in this string... ^$@%#$$, how would I do that?

gariepy
  • 3,576
  • 6
  • 21
  • 34
Evantf
  • 45
  • 1
  • 1
  • 9

2 Answers2

4

You can use split to check if symbol's in the string:

if your_str.split('$'):
    print(your_str.count('$'))

You can also use re.findall:

import re
print(len(re.findall('\$', your_str)))

It returns 0 if there is no such a symbol in the string, otherwise returns count of that symbol in the string.

But the easiest way is to check and return count if symbol is in:

print(your_str.count('$'))

It returns 0 if nothing is found.

midori
  • 4,807
  • 5
  • 34
  • 62
  • "re.findall" sounds like an interesting way to do this that I have not seen yet i will look into it. Thank you! – Evantf Feb 08 '16 at 23:20
  • Good thing about findall that you can search for a pattern, not just for a symbol – midori Feb 08 '16 at 23:22
  • That might be helpful in the future if i decide to head down that road. – Evantf Feb 08 '16 at 23:23
  • Why would you use `split` to check if a symbol is in the string? That's quite a round-about way to think of it (not to mention inefficient). Also, regex for this simple problem also seems overkill . . . `'$' in your_str` is the best way to check if a substring exists in a string. . . If you need the index where it occures, then I suppose `index = your_str.find('$')` would be the suggested method. . . – mgilson Feb 08 '16 at 23:26
  • yes find/index or just if symbol in string would work just fine, but they are already covered in other answer/comments. So i just added couple other alternative ways, findall is good when you search for a pattern, that's why i mentioned it and split is just a short, i agree that find will work faster, because it'll stop after the first occurance – midori Feb 08 '16 at 23:32
2

These are the built-in functions index and count. You can find full documentation at the official site. Please get used to doing the research on your own; the first step is to get familiar with the names of the language elements.

if my_str.index('$') != 0:
    # Found a dollar sign

print my_str.count('$')
Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    Thanks I will do more research on it! – Evantf Feb 08 '16 at 22:57
  • 2
    or `'$' in my_str` -- Which is a more idiomatic way to check for a substring in a string... – mgilson Feb 08 '16 at 23:12
  • I looked into using '$' in my_str but I like the other better... @mgilson – Evantf Feb 08 '16 at 23:21
  • 1
    `if '$' in my_str:` is a better choice. As what you are doing is assuming the character is already in the string (Returns an error if not found). Additionally, If you have the string `'$Hello$'` the if statement won't return `True` for any string starting with the character. – Steven Summers Feb 09 '16 at 03:40