A simple Python module for parsing human names into their individual components.
—
Functionality for formatting names with custom templates, generating initials, and handling capitalization of name components.
Generate initials from name components with customizable formatting and delimiters.
def initials(self):
"""
Return period-delimited initials of the first, middle and optionally last name.
Uses the initials_format template and initials_delimiter.
Returns:
str: Formatted initials string based on initials_format template
"""
def initials_list(self):
"""
Returns the initials as a list of individual letters.
Returns:
list: List of initial letters from first, middle, and last names
"""Usage Examples:
from nameparser import HumanName
# Basic initials
name = HumanName("John Michael Smith")
print(name.initials()) # 'J. M. S.'
print(name.initials_list()) # ['J', 'M', 'S']
# Custom initials format (first and middle only)
name = HumanName("Sir Bob Andrew Dole", initials_format="{first} {middle}")
print(name.initials()) # 'B. A.'
# Custom delimiter
name = HumanName("Jane Mary Johnson", initials_delimiter="-")
print(name.initials()) # 'J- M- J-'
# Names with prefixes and conjunctions
name = HumanName("Mary van der Berg")
print(name.initials()) # 'M. B.' (excludes conjunctions and prefixes)
# Titles are excluded from initials
name = HumanName("Dr. Robert James Wilson")
print(name.initials()) # 'R. J. W.' (excludes 'Dr.')Intelligent capitalization handling for names entered in all uppercase, lowercase, or mixed case.
def capitalize(self, force=None):
"""
Correct capitalization of names entered in all upper or lower case.
By default, does not adjust mixed case names unless force=True.
Args:
force (bool): Forces capitalization of mixed case strings (overrides module config)
"""Usage Examples:
from nameparser import HumanName
# Automatic capitalization of all-caps names
name = HumanName('BOB V. DE LA MACDOLE-EISENHOWER PHD')
name.capitalize()
print(str(name)) # 'Bob V. de la MacDole-Eisenhower Ph.D.'
# Automatic capitalization of lowercase names
name = HumanName('john smith jr.')
name.capitalize()
print(str(name)) # 'John Smith Jr.'
# Mixed case names are preserved by default
name = HumanName('Shirley Maclaine')
name.capitalize()
print(str(name)) # 'Shirley Maclaine' (unchanged)
# Force capitalization of mixed case
name = HumanName('Shirley Maclaine')
name.capitalize(force=True)
print(str(name)) # 'Shirley MacLaine'
# Special handling for Irish/Scottish names
name = HumanName('mary o\'connor')
name.capitalize()
print(str(name)) # "Mary O'Connor"
name = HumanName('donald macdonald')
name.capitalize()
print(str(name)) # 'Donald MacDonald'Customize how names are displayed using Python string format templates.
# String formatting properties
string_format: str # Template for full name display
initials_format: str # Template for initials display
initials_delimiter: str # Character between initialsUsage Examples:
from nameparser import HumanName
name = HumanName("Dr. John Michael Smith Jr.")
# Default format
print(str(name)) # 'Dr. John Michael Smith Jr.'
# Custom format templates
name.string_format = "{title} {first} {last}"
print(str(name)) # 'Dr. John Smith'
name.string_format = "{last}, {first} {middle}"
print(str(name)) # 'Smith, John Michael'
name.string_format = "{first} {last} ({nickname})"
name.nickname = "Johnny"
print(str(name)) # 'John Smith (Johnny)'
# Format with conditionals handled automatically
name.string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
name.nickname = "" # Empty nickname
print(str(name)) # 'Dr. John Michael Smith Jr.' (no empty parentheses)
# Multiple format styles
name.string_format = "{first} {last}"
formal = str(name) # 'John Smith'
name.string_format = "{title} {last}, {first}"
business = str(name) # 'Dr. Smith, John'Set default formatting behavior for all HumanName instances through module-level constants.
# Module-level configuration via CONSTANTS
from nameparser.config import CONSTANTS
CONSTANTS.string_format: str # Default string format template
CONSTANTS.initials_format: str # Default initials format template
CONSTANTS.initials_delimiter: str # Default initials delimiter
CONSTANTS.empty_attribute_default: str # Default value for empty attributes
CONSTANTS.capitalize_name: bool # Auto-capitalize all names
CONSTANTS.force_mixed_case_capitalization: bool # Force mixed case capitalizationUsage Examples:
from nameparser import HumanName
from nameparser.config import CONSTANTS
# Set global formatting defaults
CONSTANTS.string_format = "{first} {last}"
CONSTANTS.initials_delimiter = "-"
CONSTANTS.capitalize_name = True
# All new instances use these defaults
name1 = HumanName("john doe")
print(str(name1)) # 'John Doe' (auto-capitalized)
print(name1.initials()) # 'J- D-' (custom delimiter)
name2 = HumanName("jane smith")
print(str(name2)) # 'Jane Smith' (uses global format)
# Override global settings per instance
name3 = HumanName("bob wilson", string_format="{title} {first} {middle} {last} {suffix}")
name3.title = "Mr."
print(str(name3)) # 'Mr. Bob Wilson' (uses instance format)Handle complex formatting scenarios with empty attributes and special characters.
def collapse_whitespace(self, string):
"""
Collapse multiple spaces into single space and handle trailing commas.
Args:
string (str): String to clean up
Returns:
str: Cleaned string with normalized whitespace
"""Usage Examples:
from nameparser import HumanName
# Automatic cleanup of empty components in templates
name = HumanName("John Smith") # No title or suffix
name.string_format = "{title} {first} {last} {suffix}"
print(str(name)) # 'John Smith' (no extra spaces for empty title/suffix)
# Custom empty attribute defaults
from nameparser.config import CONSTANTS
CONSTANTS.empty_attribute_default = "N/A"
name = HumanName("John Smith")
print(name.title) # 'N/A' instead of empty string
print(name.suffix) # 'N/A' instead of empty string
# Reset to default
CONSTANTS.empty_attribute_default = ""Install with Tessl CLI
npx tessl i tessl/pypi-nameparser