Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

python - str.isdecimal() and str.isdigit() difference example

Reading python docs I have come to .isdecimal() and .isdigit() string functions and i'm not finding literature too clear on their usable distinction. Could someone supply me with code examples of where these two functions differentiate please.

Similar behaviour:

>>> str.isdecimal('1')
True
>>> str.isdigit('1')
True

>>> str.isdecimal('1.0')
False
>>> str.isdigit('1.0')
False

>>> str.isdecimal('1/2')
False
>>> str.isdigit('1/2')
False
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are differences, but they're somewhat rare*. It mainly crops up with various unicode characters, such as 2:

>>> c = 'u00B2'
>>> c.isdecimal()
False
>>> c.isdigit()
True

You can also go further down the careful-unicode-distinction rabbit hole with the isnumeric method:

>>> c = 'u00BD' # ?
>>> c.isdecimal()
False
>>> c.isdigit()
False
>>> c.isnumeric()
True

*At least, I've never encountered production code that needs to distinguish between strings that contain different types of these exceptional situations, but surely use cases exist somewhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...