Better way in Python to count string in another string

后端 未结 2 1085
滥情空心
滥情空心 2021-01-15 15:21

This code works, but reading posts on here I get the impression it is probably not a very \"Pythonic\" solution. Is there a better more efficient way to solve this specific

相关标签:
2条回答
  • 2021-01-15 16:13

    Another possible solution.

    >>> a= 'almforeachalmwhilealmleandroalmalmalm'
    >>> len(a.split('alm')) - 1
    6
    >>> q = "abcghabchjlababc"
    >>> len(q.split("abc")) - 1
    3
    
    0 讨论(0)
  • 2021-01-15 16:16

    Why not use the count method of str?

    >>> a = "abcghabchjlababc"
    >>> a.count("abc")
    3
    
    0 讨论(0)
提交回复
热议问题