String Validation Methods

Complete List of Python String Validation Methods

Python provides several built-in string methods to check if a string meets certain criteria. These methods return True or False and are useful for input validation, data cleaning, and text processing.


1. Case Checking Methods

MethodDescriptionExample
isupper()Checks if all characters are uppercase"HELLO".isupper() → True
islower()Checks if all characters are lowercase"hello".islower() → True
istitle()Checks if the string is in title case (first letter of each word capitalized)"Python Code".istitle() → True

2. Character Type Validation

MethodDescriptionExample
isalpha()Checks if all characters are alphabetic (a-z, A-Z)"Python".isalpha() → True
isdigit()Checks if all characters are digits (0-9)"123".isdigit() → True
isnumeric()Checks if all characters are numeric (includes Unicode numbers)"Ⅷ".isnumeric() → True
isdecimal()Checks if all characters are decimal (0-9 and Unicode decimal numbers)"12.5".isdecimal() → False
isalnum()Checks if all characters are alphanumeric (letters or digits)"Python123".isalnum() → True

3. Whitespace & Format Checking

MethodDescriptionExample
isspace()Checks if all characters are whitespace ( \t\n, etc.)" \t\n".isspace() → True
isprintable()Checks if all characters are printable (non-printable chars like \x00 return False)"Hello".isprintable() → True

4. ASCII & Unicode Validation

MethodDescriptionExample
isascii()Checks if all characters are ASCII (Unicode code point ≤ 127)"Python".isascii() → True
isidentifier()Checks if the string is a valid Python identifier (variable name)"var_name".isidentifier() → True

5. Specialized Checks

MethodDescriptionExample
startswith(prefix)Checks if the string starts with prefix"Hello".startswith("He") → True
endswith(suffix)Checks if the string ends with suffix"world".endswith("ld") → True

Summary Table of All Methods

MethodReturns True If
isupper()All uppercase letters
islower()All lowercase letters
istitle()Title case (each word capitalized)
isalpha()Only alphabetic letters (a-z, A-Z)
isdigit()Only digits (0-9)
isnumeric()Only numeric characters (includes Unicode)
isdecimal()Only decimal digits (0-9 and Unicode decimals)
isalnum()Only alphanumeric (letters or digits)
isspace()Only whitespace ( \t\n)
isprintable()All characters are printable
isascii()All characters are ASCII (Unicode ≤ 127)
isidentifier()Valid Python identifier (variable name)
startswith(prefix)String starts with prefix
endswith(suffix)String ends with suffix

1. s.isupper()

Checks if all characters in the string are uppercase letters.

Examples:

python

print("HELLO".isupper())  # True
print("Hello".isupper())  # False (contains lowercase 'e')
print("123ABC".isupper()) # True (numbers allowed, but letters must be uppercase)

2. s.islower()

Checks if all characters in the string are lowercase letters.

Examples:

python

print("hello".islower())  # True
print("Hello".islower())  # False (contains uppercase 'H')
print("123abc".islower()) # True (numbers allowed, but letters must be lowercase)

3. s.istitle()

Checks if the string is in title case (first letter of each word capitalized, rest lowercase).

Examples:

python

print("Python Programming".istitle())  # True
print("python programming".istitle())  # False
print("Python programming".istitle())  # False (second word not capitalized)

4. s.isalnum()

Checks if the string contains only alphanumeric characters (letters a-zA-Z, and digits 0-9).

Examples:

python

print("Python123".isalnum())  # True
print("Python_123".isalnum()) # False (contains underscore)
print("123".isalnum())        # True
print("Python".isalnum())     # True

5. s.isalpha()

Checks if the string contains only alphabetic characters (letters a-zA-Z).

Examples:

python

print("Python".isalpha())  # True
print("Python123".isalpha())  # False (contains digits)
print("123".isalpha())     # False

6. s.isspace()

Checks if the string contains only whitespace characters ( \t\n, etc.).

Examples:

python

print("   ".isspace())  # True
print("\t\n".isspace())  # True
print("Hello".isspace())  # False

7. s.isascii()

Checks if all characters in the string are ASCII (Unicode code point <= 127).

Examples:

python

print("Python".isascii())  # True
print("Pythön".isascii())  # False (contains 'ö', a non-ASCII character)
print("123#!".isascii())   # True

Summary Table

MethodChecks ForExample (True)Example (False)
isupper()All uppercase letters"HELLO""Hello"
islower()All lowercase letters"hello""Hello"
istitle()Title case (each word capitalized)"Python Code""python code"
isalnum()Only letters & digits"Python123""Python_123"
isalpha()Only letters"Python""Python123"
isspace()Only whitespace" \t\n""Hello"
isascii()Only ASCII characters"Python""Pythön"

These methods are useful for input validation, data cleaning, and text processing. Let me know if you need more examples! 😊

Unicode & ASCII-Based String Validation Methods in Python

These methods help determine if a string contains specific types of characters (digits, numbers, decimals, or alphanumeric). They are particularly useful when working with Unicode characters beyond standard ASCII (0-127).


1. isdigit()

Checks if all characters are digits (0-9 and some Unicode digits like superscripts).

Examples:

python

print("123".isdigit())        # True (ASCII digits)
print("²³⁴".isdigit())        # True (Unicode superscripts)
print("12.5".isdigit())       # False (decimal point not allowed)
print("१२३".isdigit())        # True (Devanagari digits)

2. isnumeric()

Checks if all characters are numeric (digits, fractions, Roman numerals, etc.).

Examples:

python

print("123".isnumeric())      # True (ASCII digits)
print("Ⅷ".isnumeric())        # True (Roman numeral 8)
print("¾".isnumeric())        # True (Vulgar fraction)
print("12.5".isnumeric())     # False (decimal point not numeric)

3. isdecimal()

Checks if all characters are decimal digits (0-9 and some Unicode decimals).

Examples:

python

print("123".isdecimal())      # True (ASCII digits)
print("१२३".isdecimal())      # True (Devanagari digits)
print("²³⁴".isdecimal())      # False (superscripts are digits but not decimals)
print("12.5".isdecimal())     # False (decimal point not allowed)

4. isalnum()

Checks if all characters are alphanumeric (letters a-z, A-Z, or digits 0-9, including Unicode).

Examples:

python

print("Python123".isalnum())  # True (ASCII letters & digits)
print("Pythön123".isalnum())  # True (Unicode 'ö' is alphabetic)
print("१२३abc".isalnum())     # True (Devanagari digits + ASCII letters)
print("Python_123".isalnum()) # False (underscore not allowed)

Key Differences

MethodASCII (0-9)Unicode Digits (², ¾, Ⅷ)Letters (a-z, A-Z)Others (_, ., etc.)
isdigit()✅ Yes✅ Yes (superscripts, etc.)❌ No❌ No
isnumeric()✅ Yes✅ Yes (fractions, Roman numerals)❌ No❌ No
isdecimal()✅ Yes✅ Only decimal digits (Devanagari, etc.)❌ No❌ No
isalnum()✅ Yes✅ Yes (if digits)✅ Yes❌ No

When to Use Which?

  • isdigit() → Check if a string is a valid integer (including superscripts).
  • isnumeric() → Check if a string represents any numeric value (including fractions).
  • isdecimal() → Strictly check for decimal digits (useful for number parsing).
  • isalnum() → Validate usernames, passwords (letters + digits only).

हिंदी अंक (1 से 10) के यूनिकोड कोड पॉइंट्स

हिंदी (देवनागरी) अंकों के यूनिकोड प्रतिनिधित्व की पूरी सूची यहां दी गई है:

अंकहिंदी अंकयूनिकोड कोडयूनिकोड नाम
0U+0966DEVANAGARI DIGIT ZERO
1U+0967DEVANAGARI DIGIT ONE
2U+0968DEVANAGARI DIGIT TWO
3U+0969DEVANAGARI DIGIT THREE
4U+096ADEVANAGARI DIGIT FOUR
5U+096BDEVANAGARI DIGIT FIVE
6U+096CDEVANAGARI DIGIT SIX
7U+096DDEVANAGARI DIGIT SEVEN
8U+096EDEVANAGARI DIGIT EIGHT
9U+096FDEVANAGARI DIGIT NINE
10१०U+0967 U+0966ONE + ZERO

पायथन में उपयोग के उदाहरण

python

# हिंदी अंकों को प्रिंट करना
hindi_numbers = ["०", "१", "२", "३", "४", "५", "६", "७", "८", "९"]
for num in hindi_numbers:
    print(num, end=' ')
# आउटपुट: ० १ २ ३ ४ ५ ६ ७ ८ ९

# यूनिकोड कोड का उपयोग करके
print("\n१०")  # U+0967 (१) + U+0966 (०)

# isdigit() और isnumeric() के साथ जांच
num = "१२३"
print(num.isdigit())   # True
print(num.isnumeric()) # True
print(num.isdecimal()) # True

महत्वपूर्ण तथ्य

  1. ये सभी अंक isdigit()isnumeric() और isdecimal() के लिए True रिटर्न करते हैं
  2. यूनिकोड रेंज: U+0966 से U+096F तक
  3. 10 को दर्शाने के लिए ‘१’ और ‘०’ को संयोजित करें (U+0967 + U+0966)

Telugu Numbers (0 to 10) with Unicode Codes

Here’s the complete list of Telugu numerals with their Unicode representations:

NumberTelugu NumeralUnicode CodeUnicode Name
0U+0C66TELUGU DIGIT ZERO
1U+0C67TELUGU DIGIT ONE
2U+0C68TELUGU DIGIT TWO
3U+0C69TELUGU DIGIT THREE
4U+0C6ATELUGU DIGIT FOUR
5U+0C6BTELUGU DIGIT FIVE
6U+0C6CTELUGU DIGIT SIX
7U+0C6DTELUGU DIGIT SEVEN
8U+0C6ETELUGU DIGIT EIGHT
9U+0C6FTELUGU DIGIT NINE
10౧౦U+0C67 U+0C66ONE + ZERO

Python Examples with Telugu Numbers

python

# Printing Telugu numbers 0-10
telugu_nums = ["౦", "౧", "౨", "౩", "౪", "౫", "౬", "౭", "౮", "౯"]
for num in telugu_nums:
    print(num, end=' ')
# Output: ౦ ౧ ౨ ౩ ౪ ౫ ౬ ౭ ౮ ౯ 

# Printing 10 in Telugu
print("\n౧౦")  # U+0C67 (౧) + U+0C66 (౦)

# Validation checks
num = "౧౨౩"
print(num.isdigit())    # True
print(num.isnumeric())  # True
print(num.isdecimal())  # True

Key Features

  1. All Telugu digits return True for isdigit()isnumeric(), and isdecimal()
  2. Unicode range: U+0C66 to U+0C6F
  3. To represent 10, combine ‘౧’ (ONE) and ‘౦’ (ZERO)

Additional Information

  • These digits are part of the Telugu script block in Unicode (U+0C00 to U+0C7F)
  • They are used in the Telugu language, primarily spoken in the Indian states of Andhra Pradesh and Telangana
  • The numerals follow the Hindu-Arabic numeral system positional notation

Similar Posts

  • Functions as Parameters in Python

    Functions as Parameters in Python In Python, functions are first-class objects, which means they can be: Basic Concept When we pass a function as a parameter, we’re essentially allowing one function to use another function’s behavior. Simple Examples Example 1: Basic Function as Parameter python def greet(name): return f”Hello, {name}!” def farewell(name): return f”Goodbye, {name}!” def…

  • AttributeError: ‘NoneType’ Error in Python re

    AttributeError: ‘NoneType’ Error in Python re This error occurs when you try to call match object methods on None instead of an actual match object. It’s one of the most common errors when working with Python’s regex module. Why This Happens: The re.search(), re.match(), and re.fullmatch() functions return: When you try to call methods like .group(), .start(), or .span() on None, you get this error. Example That Causes…

  • Mathematical Functions

    1. abs() Syntax: abs(x)Description: Returns the absolute value (non-negative value) of a number. Examples: python # 1. Basic negative numbers print(abs(-10)) # 10 # 2. Positive numbers remain unchanged print(abs(5.5)) # 5.5 # 3. Floating point negative numbers print(abs(-3.14)) # 3.14 # 4. Zero remains zero print(abs(0)) # 0 # 5. Complex numbers (returns magnitude) print(abs(3 +…

  • Global And Local Variables

    Global Variables In Python, a global variable is a variable that is accessible throughout the entire program. It is defined outside of any function or class. This means its scope is the entire file, and any function can access and modify its value. You can use the global keyword inside a function to modify a…

  • Operator Overloading

    Operator Overloading in Python with Simple Examples Operator overloading allows you to define how Python operators (like +, -, *, etc.) work with your custom classes. This makes your objects behave more like built-in types. 1. What is Operator Overloading? 2. Basic Syntax python class ClassName: def __special_method__(self, other): # Define custom behavior 3. Common Operator Overloading Methods Operator…

Leave a Reply

Your email address will not be published. Required fields are marked *