Strings & text
Format, split, join, parse, and clean text — everyday Python string patterns.
How to Detect if a String Contains Only ASCII in Python
This code defines a function that checks whether every character in a given string is an ASCII character (Unicode code point < 128) and demonstrates it with multiple test cases.
def is_ascii_only(text: str) -> bool:
"""Return True if all characters in text are ASCII, False otherwise."""
return all(ord(char) < 128 for char in text)
if __name__ == "__main__":
# Test cases
samples = [
"Hello, world!",
"Café au lait",
"日本語テキスト",
"ASCII only 123",
…
Normalize unicode accents to ASCII in Python
This code converts accented Unicode characters to ASCII equivalents using the standard library's unicodedata module.
import unicodedata
def normalize_accents(text: str) -> str:
"""Convert accented unicode characters to ASCII equivalents."""
decomposed = unicodedata.normalize('NFD', text)
ascii_text = ''.join(
char for char in decomposed
if unicodedata.category(char) != 'Mn'
)
return unicodedata.n…
Browse by section
Each section groups closely related Python snippets.
Strings & text — Python code examples
What you will find here
This page collects strings & text snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.