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
| Method | Description | Example |
|---|---|---|
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
| Method | Description | Example |
|---|---|---|
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
| Method | Description | Example |
|---|---|---|
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
| Method | Description | Example |
|---|---|---|
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
| Method | Description | Example |
|---|---|---|
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
| Method | Returns 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-z, A-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-z, A-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
| Method | Checks For | Example (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
| Method | ASCII (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) के यूनिकोड कोड पॉइंट्स
हिंदी (देवनागरी) अंकों के यूनिकोड प्रतिनिधित्व की पूरी सूची यहां दी गई है:
| अंक | हिंदी अंक | यूनिकोड कोड | यूनिकोड नाम |
|---|---|---|---|
| 0 | ० | U+0966 | DEVANAGARI DIGIT ZERO |
| 1 | १ | U+0967 | DEVANAGARI DIGIT ONE |
| 2 | २ | U+0968 | DEVANAGARI DIGIT TWO |
| 3 | ३ | U+0969 | DEVANAGARI DIGIT THREE |
| 4 | ४ | U+096A | DEVANAGARI DIGIT FOUR |
| 5 | ५ | U+096B | DEVANAGARI DIGIT FIVE |
| 6 | ६ | U+096C | DEVANAGARI DIGIT SIX |
| 7 | ७ | U+096D | DEVANAGARI DIGIT SEVEN |
| 8 | ८ | U+096E | DEVANAGARI DIGIT EIGHT |
| 9 | ९ | U+096F | DEVANAGARI DIGIT NINE |
| 10 | १० | U+0967 U+0966 | ONE + 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
महत्वपूर्ण तथ्य
- ये सभी अंक
isdigit(),isnumeric()औरisdecimal()के लिएTrueरिटर्न करते हैं - यूनिकोड रेंज: U+0966 से U+096F तक
- 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:
| Number | Telugu Numeral | Unicode Code | Unicode Name |
|---|---|---|---|
| 0 | ౦ | U+0C66 | TELUGU DIGIT ZERO |
| 1 | ౧ | U+0C67 | TELUGU DIGIT ONE |
| 2 | ౨ | U+0C68 | TELUGU DIGIT TWO |
| 3 | ౩ | U+0C69 | TELUGU DIGIT THREE |
| 4 | ౪ | U+0C6A | TELUGU DIGIT FOUR |
| 5 | ౫ | U+0C6B | TELUGU DIGIT FIVE |
| 6 | ౬ | U+0C6C | TELUGU DIGIT SIX |
| 7 | ౭ | U+0C6D | TELUGU DIGIT SEVEN |
| 8 | ౮ | U+0C6E | TELUGU DIGIT EIGHT |
| 9 | ౯ | U+0C6F | TELUGU DIGIT NINE |
| 10 | ౧౦ | U+0C67 U+0C66 | ONE + 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
- All Telugu digits return
Trueforisdigit(),isnumeric(), andisdecimal() - Unicode range: U+0C66 to U+0C6F
- 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