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

  • Indexing and Slicing in Python Lists Read

    Indexing and Slicing in Python Lists Read Indexing and slicing are fundamental operations to access and extract elements from a list in Python. 1. Indexing (Accessing Single Elements) Example 1: Basic Indexing python fruits = [“apple”, “banana”, “cherry”, “date”, “fig”] # Positive indexing print(fruits[0]) # “apple” (1st element) print(fruits[2]) # “cherry” (3rd element) # Negative indexing print(fruits[-1]) # “fig”…

  • Special Sequences in Python

    Special Sequences in Python Regular Expressions – Detailed Explanation Special sequences are escape sequences that represent specific character types or positions in regex patterns. 1. \A – Start of String Anchor Description: Matches only at the absolute start of the string (unaffected by re.MULTILINE flag) Example 1: Match only at absolute beginning python import re text = “Start here\nStart…

  • Programs

    Weekly Wages Removing Duplicates even ,odd Palindrome  Rotate list Shuffle a List Python random Module Explained with Examples The random module in Python provides functions for generating pseudo-random numbers and performing random operations. Here’s a detailed explanation with three examples for each important method: Basic Random Number Generation 1. random.random() Returns a random float between 0.0 and 1.0 python import…

  • What is PyCharm? Uses, History, and Step-by-Step Installation Guide

    What is PyCharm? PyCharm is a popular Integrated Development Environment (IDE) specifically designed for Python development. It is developed by JetBrains and is widely used by Python developers for its powerful features, ease of use, and support for various Python frameworks and tools. PyCharm is available in two editions: Uses of PyCharm PyCharm is a…

  • What is list

    In Python, a list is a built-in data structure that represents an ordered, mutable (changeable), and heterogeneous (can contain different data types) collection of elements. Lists are one of the most commonly used data structures in Python due to their flexibility and dynamic nature. Definition of a List in Python: Example: python my_list = [1, “hello”, 3.14,…

  • Case Conversion Methods in Python

    Case Conversion Methods in Python (Syntax + Examples) Python provides several built-in string methods to convert text between different cases (uppercase, lowercase, title case, etc.). Below are the key methods with syntax and examples: 1. upper() – Convert to Uppercase Purpose: Converts all characters in a string to uppercase.Syntax: python string.upper() Examples: python text = “Hello, World!”…

Leave a Reply

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