Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm experiencing difficulty attempting to execute this code, I need the user to enter a value, the program checks in the event that that value is a string, at that point it returns the length. In the event that the value contains whitespaces the programs eliminate the whitespace and print the length. Be that as it may, If it contain any integer qualities the program returns "No Integers is Allowed"

Look at the code:

def length(Name):

    long = len(Name)

    return long

new_length = input("Please Enter Your name You can use Spaces: ")

value1 = new_length

if value1.isspace() == True:

  print("This is Before Removing Spaces: " + value1)

  value2 = value1.replace(" ", "")

  print("This is After Removing Spaces: " + value2)

elif value1.isalpha() == True: 

  print("Here is The Length: ", length(value1))

elif value1.isdigit() == True:

    print("Integers are not allowed! ")

else:

    print("There's someting wrong with  "+ value1)

Can anyone help me?

1 Answer

0 votes
by (26.4k points)

You can utilize the re module in Python to check for the white spaces in your string. It returns True if there are any white spaces and False in any case.

import re

def length(Name):

    long = len(Name)

    return long

new_length = input("Please Enter Your name You can use Spaces: ")

value1 = new_length

if re.search('\s', value1):

  print("This is Before Removing Spaces: " + value1)

  value2 = value1.replace(" ", "")

  print("This is After Removing Spaces: " + value2)

  print("Here is The Length: ", length(value2))

elif value1.isalpha() == True: 

  print("Here is The Length: ", length(value1))

elif value1.isdigit() == True:

    print("Integers are not allowed! ")

else:

    print("There's someting wrong with  "+ value1)

Interested to learn python in detail? Come and Join the python course.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)
0 votes
2 answers
0 votes
1 answer
asked Mar 22, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer

Browse Categories

...