Python Program to test if the String only Numbers and Alphabets


When working with strings in Python, it is often necessary to validate whether a string contains only numbers and alphabets or if it includes other special characters. String validation is crucial in various scenarios such as input validation, data processing, and filtering.

In this article, we will explore a Python program to test if a given string consists of only alphanumeric characters. We will discuss the criteria for a valid string, provide examples of valid and invalid strings, and present an efficient approach to solve this problem using built-in string methods.

Understanding the Problem

Before we dive into solving the problem, let's define the criteria for a valid string that contains only numbers and alphabets −

  • The string should not contain any whitespace or special characters.

  • The string should consist of alphanumeric characters (a-z, A-Z, and 0-9).

  • The string should have at least one character.

Our task is to write a Python program that takes a string as input and checks whether it meets these criteria. We need to return True if the string contains only numbers and alphabets, and False otherwise.

To solve this problem, we will utilize Python's built-in string methods and logical operations. We will walk through the approach and algorithm to achieve this validation.

Approach and Algorithm

To determine if a string contains only numbers and alphabets, we can follow a straightforward approach. We will iterate through each character of the string and check if it is alphanumeric. If we encounter any non-alphanumeric character, we will return False. If all characters pass the alphanumeric check, we will return True.

Here's the step-by-step algorithm to solve the problem −

  • Define a function that takes a string as input.

  • Iterate through each character in the string.

  • For each character, check if it is alphanumeric using the isalnum() method.

  • If any character is found to be non-alphanumeric, return False.

  • If all characters pass the check, return True.

Now that we have a clear approach and algorithm in mind, let's implement the solution in Python.

Implementation

Now, let's implement the Python program to test if a string contains only numbers and alphabets. We will follow the approach and algorithm discussed earlier.

def is_alphanumeric(string):
    for char in string:
        if not char.isalnum():
            return False
    return True

In the above code, we define a function is_alphanumeric that takes a string as input. We iterate through each character of the string using a for loop. For each character, we use the isalnum() method to check if it is alphanumeric. If any character is found to be non-alphanumeric, we immediately return False. If all characters pass the check, we return True.

Let's test the program with some examples.

print(is_alphanumeric("Hello123"))  # Output: True
print(is_alphanumeric("Hello World"))  # Output: False
print(is_alphanumeric("12345"))  # Output: True
print(is_alphanumeric("12345!"))  # Output: False

In the above examples, we test the program with different strings. The expected output is provided as comments.

Now, let's move on to the next section to discuss the output and analyze the program's performance.

Performance Analysis

Let's analyze the output of the program and discuss its performance.

The is_alphanumeric function takes a string as input and returns True if the string contains only numbers and alphabets, and False otherwise.

For example, when we test the function with the string "Hello123", it contains both letters and numbers, so the function returns True. On the other hand, when we test it with the string "Hello World", it contains a space character, which is not alphanumeric, so the function returns False.

The function is designed to iterate through each character of the string and use the isalnum() method to check if it is alphanumeric. This approach has a time complexity of O(n), where n is the length of the string. It performs a linear scan of the string, checking each character individually.

The space complexity of the function is O(1), as it does not require any additional data structures that grow with the input size.

Overall, the program provides an efficient solution to determine if a string contains only numbers and alphabets, with a linear time complexity based on the length of the string.

Conclusion

In this article, we have explored how to write a Python program to test if a string contains only numbers and alphabets. We started by understanding the problem statement and discussing the approach to solve it. Then, we implemented a function is_alphanumeric that checks each character of the string using the isalnum() method and returns True if all characters are alphanumeric.

We have seen how to use the function with various test cases and discussed the expected outputs. Additionally, we analyzed the performance of the program, noting its time complexity and space complexity.

By using this program, you can easily determine whether a given string contains only numbers and alphabets, which can be useful in scenarios where you need to validate user input or process specific types of data.

Updated on: 10-Aug-2023

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements