Saturday, December 28, 2024
Google search engine
HomeLanguagesPython | First alphabet index

Python | First alphabet index

Sometimes, while working with Python strings we can have a problem in which we need to extract the first index of alpha character from string. This can have application in day-day programming. Lets discuss certain ways in which this task can be performed. 

Method #1: Using loop + regex The combination of above functionalities can be used to perform this task. In this, we employ loop to loop through the string and regex is used to filter out for alphabets in characters. 

Python3




# Python3 code to demonstrate working of
# First alphabet index
# Using loop + regex
import re
 
# initializing string
test_str = "34#$g67fg"
 
# printing original string
print("The original string is : " + test_str)
 
# First alphabet index
# Using loop + regex
res = None
temp = re.search(r'[a-z]', test_str, re.I)
if temp is not None:
    res = temp.start()
 
# printing result
print("Index of first character : " + str(res))


Output : 

The original string is : 34#$g67fg
Index of first character : 4

Time complexity: O(n), where n is the length of the input string “test_str”. 
Auxiliary space: O(1), which means it uses a constant amount of extra memory space, regardless of the size of the input string “test_str”

Method #2: Using find() + next() + filter() + isalpha() The combination of above methods can also be used to perform this task. In this, we check for alphabets using isalpha(). The task of finding element is done by find(). The next() returns the 1st occurrence. 

Python3




# Python3 code to demonstrate working of
# First alphabet index
# Using find() + next() + filter() + isalpha()
import re
 
# initializing string
test_str = "34#$g67fg"
 
# printing original string
print("The original string is : " + test_str)
 
# First alphabet index
# Using find() + next() + filter() + isalpha()
res = test_str.find(next(filter(str.isalpha, test_str)))
 
# printing result
print("Index of first character : " + str(res))


Output : 

The original string is : 34#$g67fg
Index of first character : 4

Method #3: Using loop + ord() The combination of above method can be used to solve the task. In this, we iterate over string and check the ascii value with the help of ord method. If the ascii value is in range of alphabets than return the index of character. 

Python3




# Python3 code to demonstrate working of
# First alphabet index
# Using loop + ord
 
# initializing string
test_str = "3g4#$67fg"
 
# printing original string
print("The original string is : " + test_str)
 
# First alphabet index
# Using lopp + ord
ans = -1;
for i in test_str:
    temp = ord(i)
    if (65 <= temp <= 90) or (97 <= temp <= 122):
        ans = test_str.index(i);
        break;
 
 
# printing result
print("Index of first character : " + str(ans))


Output:

The original string is : 3g4#$67fg
Index of first character : 1

Method #4: Using index() and without isalpha() method

Python3




# Python3 code to demonstrate working of
# First alphabet index
 
# initializing string
test_str = "34#$g67fg"
 
# printing original string
print("The original string is : " + test_str)
 
# First alphabet index
alphabets="abcdefghijklmnopqrstuvwxyz"
res = 0
for i in test_str:
    if i in alphabets:
        res=test_str.index(i)
        break
 
# printing result
print("Index of first character : " + str(res))


Output

The original string is : 34#$g67fg
Index of first character : 4

Time Complexity: O(n)
Auxiliary Space: (n)

Method #5: Using re.search() with ignorecase flag

Python3




#Python3 code to demonstrate working of
#First alphabet index
#Using re.search() with ignorecase flag
import re
 
#initializing string
test_str = "34#$g67fg"
 
#printing original string
print("The original string is : " + test_str)
 
#First alphabet index
#Using re.search() with ignorecase flag
res = None
temp = re.search(r'[a-zA-Z]', test_str)
if temp is not None:
  res = temp.start()
 
#printing result
print("Index of first character : " + str(res))


Output

The original string is : 34#$g67fg
Index of first character : 4

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #6: Using a list comprehension and isalpha()

Python3




test_str = "34#$g67fg"
# Use a list comprehension to generate a list of (index, character) pairs for all characters in the string
# that satisfy the condition c.isalpha()
matches = [(i, c) for i, c in enumerate(test_str) if c.isalpha()]
# Take the first pair from the list and extract the index (i.e. the first index of an alphabet character)
res = matches[0][0]
print("Index of first character : " + str(res))


Output

Index of first character : 4

Time Complexity:

  • The list comprehension creates a list of length <= len(test_str) containing only characters that satisfy the condition c.isalpha().
  • Indexing the list to get the first pair is a constant-time operation.
  • Therefore, the time complexity of this method is O(len(test_str)).

Auxiliary Space:

  • The list comprehension creates a new list of length <= len(test_str) containing only characters that satisfy the condition c.isalpha().
  • Therefore, the auxiliary space complexity of this method is O(len(test_str)).

Method #7: Using a generator expression and isalpha()

Use the enumerate() function to iterate over the characters in test_str along with their indices. The isalpha() method is then called on each character to determine if it is an alphabet. The generator expression (i for i, c in enumerate(test_str) if c.isalpha()) produces a sequence of indices where the corresponding characters are alphabets. The next() function is then used to retrieve the first index from this sequence, or -1 if the sequence is empty.

Python3




test_str = "3g4#$67fg"
ans = next((i for i, c in enumerate(test_str) if c.isalpha()), -1)
print("Index of first character : " + str(ans))


Output

Index of first character : 1

Time Complexity: O(n), where n is the length of the string.
Auxiliary space: O(1)

Method 8 : Using lambda function and filter() method

  1. Define a lambda function that returns True if the given character is an alphabet, else False.
  2. Use the “filter()” method to filter out non-alphabetic characters from the given string “test_str”.
  3. Convert the filtered result to a list.
  4. If the list is not empty, print the index of its first element in the original string “test_str” using the “index()” method.

Python3




# initializing string
test_str = "34#$g67fg"
 
# printing original string
print("The original string is : " + test_str)
 
# First alphabet index
# Using lambda function and filter() method
alpha_list = list(filter(lambda char: char.isalpha(), test_str))
 
if alpha_list:
    res = test_str.index(alpha_list[0])
else:
    res = None
 
# printing result
print("Index of first character : " + str(res))


Output

The original string is : 34#$g67fg
Index of first character : 4

The time complexity of the above code is O(n), where n is the length of the given string “test_str”.

The auxiliary space used by the code is O(m), where m is the number of alphabetic characters in the string “test_str”. 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments