Knowee
Questions
Features
Study Tools

Check NumbersSend FeedbackPython program to check if a string has at least one letter and one numberGiven a string in Python. The task is to check whether the string has at least one letter(character) and one number. Return “True” if the given string fulfils the above condition else return “False” (without quotes).Input FormatStringOutput FormatBooleanInput 1welcome2ourcountry34Output 1TrueInput 2stringwithoutnumOutput 2False

Question

Check NumbersSend FeedbackPython program to check if a string has at least one letter and one numberGiven a string in Python. The task is to check whether the string has at least one letter(character) and one number. Return “True” if the given string fulfils the above condition else return “False” (without quotes).Input FormatStringOutput FormatBooleanInput 1welcome2ourcountry34Output 1TrueInput 2stringwithoutnumOutput 2False

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, you can use the isalpha() and isdigit() functions in Python. Here is a step-by-step guide on how to do it:

  1. Initialize two boolean variables, has_digit and has_alpha, to False. These will be used to track whether the string has at least one digit and one letter.

  2. Iterate over each character in the string using a for loop.

  3. Inside the loop, check if the current character is a digit using the isdigit() function. If it is, set has_digit to True.

  4. Similarly, check if the current character is a letter using the isalpha() function. If it is, set has_alpha to True.

  5. After the loop, check if both has_digit and has_alpha are True. If they are, return True. Otherwise, return False.

Here is a Python function that implements the above steps:

def check_string(s):
    has_digit = False
    has_alpha = False

    for char in s:
        if char.isdigit():
            has_digit = True
        if char.isalpha():
            has_alpha = True

    return has_digit and has_alpha

You can use this function to check if a string has at least one letter and one number. For example, check_string('welcome2ourcountry34') will return True, while check_string('stringwithoutnum') will return False.

This problem has been solved

Similar Questions

Write a program to check if string contain only Numbers

Check whether the given character is an alphabet or a numeric character or special characterInput Format:Enter a Character as inputOutput Format:Print the output as "NUMBER" or "ALPHABET" or "SPECIAL CHARACTER"Constraints:0 <= CHARACTER <= 2^7Sample Input 1:7Sample Output 1:NUMBERSample Input 2:aSample Output 2:ALPHABET

Input FormatA single line containing a string .ConstraintsOutput FormatIn the first line, print True if has any alphanumeric characters. Otherwise, print False.In the second line, print True if has any alphabetical characters. Otherwise, print False.In the third line, print True if has any digits. Otherwise, print False.In the fourth line, print True if has any lowercase characters. Otherwise, print False.In the fifth line, print True if has any uppercase characters. Otherwise, print False.

You are given a positive integer n.A binary string x is valid if all substrings of x of length 2 contain at least one "1".Return all valid strings with length n, in any order. Example 1:Input: n = 3Output: ["010","011","101","110","111"]Explanation:The valid strings of length 3 are: "010", "011", "101", "110", and "111".Example 2:Input: n = 1Output: ["0","1"]Explanation:The valid strings of length 1 are: "0" and "1".

Check whether the given character is a Vowel or ConsonantInput Format:Enter a Character as input  Output Format:Print the output as "Vowel" or "Consonant"  Constraints:0 <= letter <= 2^7Sample Input 1:tSample Output 1:Consonant

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.