Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Python string isdecimal() Method

Posted in Python Tutorial
Updated on Jan 11, 2024
By Mari Selvan
👁ī¸ 41 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
Python string isdecimal() Method

Photo Credit to CodeToFun

🙋 Introduction

In Python, string manipulation is a fundamental aspect of programming.

The isdecimal() method is a built-in string method that allows you to check if a string contains only decimal characters.

In this tutorial, we'll explore the usage and functionality of the isdecimal() method in Python.

💡 Syntax

The syntax for the isdecimal() method is as follows:

Syntax
Copied
Copy To Clipboard
string.isdecimal()

string: The string to be checked for decimal characters.

📄 Example 1

Let's dive into some examples to illustrate how the isdecimal() method works.

isdecimal.py
Copied
Copy To Clipboard
num_str = "12345"
result1 = num_str.isdecimal()
print("Example 1:", result1)

đŸ’ģ Testing the Program

Output
Example 1: True

🧠 How the Program Works

In Example 1, the isdecimal() method checks if the string "12345" contains only decimal characters, and the result is True.

📄 Example 2

Let's dive into another examples to illustrate how the isdecimal() method works.

isdecimal.py
Copied
Copy To Clipboard
text_str = "Hello123"
result2 = text_str.isdecimal()
print("Example 2:", result2)

đŸ’ģ Testing the Program

Output
Example 2: False

🧠 How the Program Works

In Example 2, the string "Hello123" contains non-decimal characters, so the result of isdecimal() is False.

↩ī¸ Return Value

The isdecimal() method returns a boolean value:

  • True: if all characters in the string are decimal.
  • False: if the string contains at least one non-decimal character.

📚 Common Use Cases

The isdecimal() method is useful when you need to validate user input or verify that a string represents a decimal number before performing mathematical operations.

📝 Notes

  • The isdecimal() method only considers characters that are considered decimal digits in Unicode. This includes digits from various scripts, not just the ASCII digits 0-9.
  • The method returns False for strings containing digits with special meanings, such as superscript digits.

đŸŽĸ Optimization

The isdecimal() method is optimized for efficiency. However, keep in mind that it checks the entire string, so for very long strings, consider its performance impact.

🎉 Conclusion

The isdecimal() method in Python is a valuable tool for checking whether a string consists of only decimal characters. It provides a quick and straightforward way to validate and process numeric input.

Feel free to experiment with different strings to explore the behavior of the isdecimal() method further. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
3 months ago

If you have any doubts regarding this article (Python string isdecimal() Method), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy