Find position of a character in given string - Python Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string and a character, our task is to find the first position of the occurrence of the character in the string using Python. For example, consider a string s = "Geeks" and character k = 'e', in the string s, the first occurrence of the character 'e' is at index1. Let's look at various methods of solving this problem:Using Regular Expressions (re.search())Regex (Regular Expressions) are patterns used to match sequences in text. With re.search(), we can find the first match of a pattern and get its position. Python import re s = 'Geeksforgeeks' k = 'for' match = re.search(k, s) print("starting index", match.start()) print("start and end index", match.span()) Outputstarting index 5 start and end index (5, 8) Explanation:re.search() returns the first match of the pattern..start() gives the index of the first character..span() gives a tuple (start, end) of the matched portion.Using index()The index() method returns the index of the first occurrence of a character. If not found, it raises a ValueError. Python s = 'xyze' k = 'b' try: pos = s.index(k) print(pos) except ValueError: print(-1) Output-1 Explanation:index() finds the first occurrence and returns its index.If the character isn’t found, it raises an error-handled using try-except.Using a LoopWe can also manually loop through the string to find the first match. Python s = 'GeeksforGeeks' k = 'o' res = -1 for i in range(len(s)): if s[i] == k: res = i break print(res) Output6 Explanation:Loop checks each character.If matched, it returns the index and exits early with break.Using find()find() method returns the index of the first match. If not found, it returns -1. Python s1 = 'abcdef' s2 = 'xyze' k = 'b' print(s1.find(k)) print(s2.find(k)) Output1 -1 Explanation:find() returns index of the first match.If no match is found, it returns -1 instead of raising an error.Using enumerate() with next()This approach uses list comprehension and lazy evaluation to get the first index where the character matches. Python def find_pos(s, k): try: return next(i for i, c in enumerate(s) if c == k) except StopIteration: return -1 s = 'xybze' k = 'b' print(find_pos(s, k)) Output2 Explanation:enumerate() gives index-character pairs.next() returns the first matching index.If not found, StopIteration is caught and -1 is returned.Related articles:rfind() methodre.search() methodenumerate() methodnext() Comment More infoAdvertise with us Next Article Split String into List of characters in Python G garg_ak0109 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in 2 min read Get Last N characters of a string - Python We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh 2 min read Replace a String character at given index in Python In Python, strings are immutable, meaning they cannot be directly modified. We need to create a new string using various methods to replace a character at a specific index. Using slicingSlicing is one of the most efficient ways to replace a character at a specific index.Pythons = "hello" idx = 1 rep 2 min read Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t 2 min read Python - Extract only characters from given string To extract only characters (letters) from a given string we can use various easy and efficient methods in Python. Using str.isalpha() in a Loop str.isalpha() method checks if a character in a string is an alphabetic letter. Using a loop, we can iterate through each character in a string to filter ou 2 min read Python - Characters Index occurrences in String Sometimes, while working with Python Strings, we can have a problem in which we need to check for all the characters indices. The position where they occur. This kind of application can come in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using set() + reg 6 min read Like