codingstreets
Search
Close this search box.
python-string-methods-isdigit-isidentifier-islower-isnumeric-isprintable

Python String Methods isdigit(), isidentifier(), islower(), isnumeric(), isprintable()

In this article, learn about essential Python string methods: isdigit(), isidentifier(), islower(), isnumeric(), and isprintable(). Discover how these methods can help you validate and manipulate strings effortlessly. Enhance your Python programming skills with practical examples and tips.

Before moving ahead, let’s take a look at Python String Methods.

Table of Contents

isdigit() method

isdigit() returns True, if all characters are numeric (0-9), else returns False.

Example: Check if all the characters are digits.

				
					text = '12345'
is_digits = text.isdigit()

print('The text is digits:', is_digits)

#output:
The text is digits: True

				
			

Example: Check if all the characters are digits.

				
					text = '12345 Hello'
is_digits = text.isdigit()

print('The text is digits:', is_digits)

#output:
The text is digits: False
				
			

Note: Isdigit() method returns False if using fractions and Roman numerals.

isidentifier() method

isidentifier() returns True, if the string is a valid identifier, otherwise False.

Syntax – string.isidentifier()

Parameter Values – No.

Valid identifier – A valid identifier is the combination of a-z, 0-9, and underscore (_). It cannot start with the number and cannot contain the whitespace.

Example: Check if the string is a valid identifier.

				
					text = 'hello'
is_valid = text.isidentifier()

print(is_valid)

#output:
True
				
			

Example: Check if the string is a valid identifier.

				
					text = '122'
is_valid = text.isidentifier()

print(is_valid)

#output:
False

				
			

Example: Check if the string is a valid identifier.

				
					text = 'hello_python123'
is_valid = text.isidentifier()

print(is_valid)

#output:
True
				
			

Example: Check if the string is a valid identifier.

				
					text = 'hello_python 123'
is_valid = text.isidentifier()

print(is_valid)

#output:
False
				
			

Example: Check if the string is a valid identifier.

				
					text = '_hello_python'
is_valid = text.isidentifier()

print(is_valid)

#output:
True
				
			

islower() method

islower() returns True, if all characters (a-z) are in lowercase, else returns False.

Syntax – string.lower()

Parameter Values – No.

Example: Check if all characters of the string are in lowercase.

				
					text1 = 'hello, python'
text2 = 'HELLO, PYTHON'

print(text1.islower())
print(text2.islower())

#output:
True
False
				
			

Note: islower() method doesn’t convert numbers, symbols, and space into lowercase.

Example: Check if all characters of the string are in lowercase.

				
					text1 = '12@$^^&&*Y'
print(text1.islower())

#output:
False
				
			

isnumeric() method

isnumeric() – returns True, if all characters are numeric 0-9, else returns False. It also considered exponent values as numeric values.

Example: Check if all the characters are numeric.

				
					text = '12'
is_numeric = text.isnumeric()
print(is_numeric)

#output:
True
				
			

Example: Check if all the characters are numeric.

				
					text = '12!'
is_numeric = text.isnumeric()
print(is_numeric)

#output:
False
				
			

isprintable() method

isprintable() returns True, if all characters are printable, else returns False.

Printable characters – Printable characters are the combination of the alphabet (a-z), number (0-9), symbol ($,@,#), whitespace, punctuation, and underscore (_).

Example: Check if all the characters are printable.

				
					text = 'Hello World'
is_printable = text.isprintable()

print(is_printable)

#output:
True
				
			

Example: Check if all the characters are printable.

				
					text = 'Hello World123'
is_printable = text.isprintable()

print(is_printable)

#output:
True
				
			

Example: Check if all the characters are printable.

				
					text = 'Hello \n World'
is_printable = text.isprintable()

print(is_printable)


#output:
False
				
			

Conclusion

By understanding and utilizing these methods effectively, you can perform various string operations, validate user inputs, and enhance the reliability of your Python programs. Each method has its specific use cases and can greatly simplify string-related tasks.

Remember to practice examples to fully grasp the functionality and versatility of these string methods. With practice and experience, you’ll be able to harness the power of these methods to handle strings efficiently in your Python projects.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on