Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Python. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for String Validators in Python-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Python
Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming. It was created by Guido van Rossum, and first released on February 20, 1991.
Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. It is also used to create various machine learning algorithm, and helps in Artificial Intelligence. Python is a general purpose language, meaning it can be used to create a variety of different programs and isn’t specialized for any specific problems. This versatility, along with its beginner-friendliness, has made it one of the most-used programming languages today. A survey conducted by industry analyst firm RedMonk found that it was the most popular programming language among developers in 2020.
Link for the Problem – String Validators in Python – HackerRank Solution
String Validators in Python – HackerRank Solution
Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.
str.isalnum()
This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
>>> print 'ab123'.isalnum() True >>> print 'ab123#'.isalnum() False
str.isalpha()
This method checks if all the characters of a string are alphabetical (a-z and A-Z).
>>> print 'abcD'.isalpha() True >>> print 'abcd1'.isalpha() False
str.isdigit()
This method checks if all the characters of a string are digits (0-9).
>>> print '1234'.isdigit() True >>> print '123edsd'.isdigit() False
str.islower()
This method checks if all the characters of a string are lowercase characters (a-z).
>>> print 'abcd123#'.islower() True >>> print 'Abcd123#'.islower() False
str.isupper()
This method checks if all the characters of a string are uppercase characters (A-Z).
>>> print 'ABCD123#'.isupper() True >>> print 'Abcd123#'.isupper() False
Task
You are given a string .
Your task is to find out if the string contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.
Input Format
A single line containing a string .
Constraints
Output Format
In 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
.
Sample Input
qA2
Sample Output
True True True True True
String Validators in Python – HackerRank Solution
def fun1(s): for i in range(len(s)): if(s[i].isalnum()): return True; break; return False; def fun2(s): for i in range(len(s)): if(s[i].isalpha()): return True; break; return False; def fun3(s): for i in range(len(s)): if(s[i].isdigit()): return True; break; return False; def fun4(s): for i in range(len(s)): if(s[i].islower()): return True; break; return False; def fun5(s): for i in range(len(s)): if(s[i].isupper()): return True; break; return False; # String Validators in Python - HackerRank Solution END if __name__ == '__main__': s = raw_input() # String Validators in Python - HackerRank Solution START flagalphanum = fun1(s) alphabetical = fun2(s) digits = fun3(s) lowercase = fun4(s) uppercase = fun5(s) print(flagalphanum) print(alphabetical) print(digits) print(lowercase) print(uppercase)
Hi there! Do you use Twitter? I’d like to follow you if that would be ok. I’m absolutely enjoying your blog and look forward to new posts.