4 examples of 'find letters in string python' in Python

Every line of 'find letters in string python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
1def count_letters(string):
2 freq, limit, i = {}, len(string), 0
3 while i < limit:
4 if string[i] in freq:
5 freq[string[i]] += 1
6 else:
7 freq[string[i]] = 1
8 i += 1
9 return freq
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

5def reverseOnlyLetters(self, S):
6 """
7 :type S: str
8 :rtype: str
9 """
10 def getNext(S):
11 for i in reversed(xrange(len(S))):
12 if S[i].isalpha():
13 yield S[i]
14
15 result = []
16 letter = getNext(S)
17 for i in xrange(len(S)):
18 if S[i].isalpha():
19 result.append(letter.next())
20 else:
21 result.append(S[i])
22 return "".join(result)
2def firstUniqChar(self, s):
3 """
4 :type s: str
5 :rtype: int
6 """
7 counts = [0] * 26
8 for item in s:
9 counts[ord(item) - ord('a')] += 1
10 for (index, item) in enumerate(s):
11 if counts[ord(item) - ord('a')] == 1:
12 return index
13 return -1
42def firstUniqChar(self, s):
43 """
44 :type s: str
45 :rtype: int
46 """
47 alphabet = 'abcdefghijklmnopqrstyvwxyz'
48 index = len(s)
49 for c in alphabet:
50 if c in s:
51 l = s.find(c)
52 r = s.rfind(c)
53 else:
54 continue
55 if l == r and l < index:
56 index = l
57 if index == len(s):
58 return -1
59 return index

Related snippets