Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What are string validation functions in Python?

Eman Kashif

Python is an open-source high-level programming language that provides a range of modules and functions. A number of methods exist to manipulate and validate string variables. Validation is an important operation in testing systems. Let’s take a look at some of these methods.

isalnum()

To check whether all the characters in a string are alphanumeric, we use the isalnum() method. isalnum() returns True if all the characters are either alphabetical (a-z) or numerical (0-9), and False if any other character (including the space character) is present in the string.

#initialize strings
string1 = "12rocks"
string2 = "interest rate of 2.5%"

#test strings
x = string1.isalnum()
y = string2.isalnum()

#print results
print("String 1:",x)
print("String 2:",y)

islower()

To check whether all the characters in a string are lowercase, we use the islower() method. Only alphabetical characters are checked. islower() returns True if all the characters are lowercase.

#initialize strings
string1 = "she has 12 rocks"
string2 = "Interest rate of 2.5%"

#test strings
x = string1.islower()
y = string2.islower()

#print results
print("String 1:",x)
print("String 2:",y)

isprintable()

To check whether all characters in a string are printable, we use the isprintable() method. isprintable() returns True if all characters are printable, and False if non-printable characters, such as line feed and carriage return, are included in the string.

#initialize strings
string1 = "She had a salary increase of 10%!"
string2 = "She had a salary increase\n of 10%!"

#test strings
x = string1.isprintable()
y = string2.isprintable()

#print results
print("String 1:",x)
print("String 2:",y)

RELATED TAGS

python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring