
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to compare string and number in Python?
Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).
When you order a numeric and a non-numeric type, the numeric type comes first.
If you have a number in a str object, you can simply convert it to a float or an int using their respective constructors. For example,
i = 100 j = "12" int_j = int(j) print(int_j < i)
This will give the output:
True
- Related Articles
- How to compare Python string formatting: % with .format?
- How to compare String equality in Java?
- How to Compare two String in Java?
- How to use compare string in android?
- How to compare regular expressions in Perl and Python?
- How to compare numbers in Python?
- How to compare files in Python
- How do I compare String and Boolean in JavaScript?
- How to compare date strings in Python?
- How to compare two lists in Python?
- How to compare two dates in String format in Java?
- How do we compare String in Java
- How to compare calendar.timegm() vs. time.mktime() in Python?
- How to compare two images in OpenCV Python?
- How will you compare namespaces in Python and C++?

Advertisements