🔍 Code Extractor

function capitalize

Maturity: 35

Capitalizes the first letter of a string, leaving the rest of the string unchanged.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1812 - 1819
Complexity:
simple

Purpose

This function provides a simple string manipulation utility that capitalizes only the first character of a given string. Unlike Python's built-in str.capitalize() method which lowercases all other characters, this function preserves the original casing of characters after the first one. It handles empty strings and None values gracefully by returning them unchanged.

Source Code

def capitalize(string):
    """
    Capitalizes the first letter of a string.
    """
    if string:
        return string[0].upper() + string[1:]
    else:
        return string

Parameters

Name Type Default Kind
string - - positional_or_keyword

Parameter Details

string: The input string to capitalize. Can be any string value, including empty strings or None. If the string is empty or None, it will be returned unchanged. If the string has at least one character, only the first character will be uppercased while all other characters remain in their original case.

Return Value

Returns a new string with the first character converted to uppercase and all remaining characters unchanged. If the input is an empty string or None/falsy value, returns the input unchanged. Return type is str or the same type as the input if falsy.

Usage Example

# Basic usage
result1 = capitalize('hello world')
print(result1)  # Output: 'Hello world'

# Preserves existing capitalization
result2 = capitalize('hELLO WORLD')
print(result2)  # Output: 'HELLO WORLD'

# Handles empty strings
result3 = capitalize('')
print(result3)  # Output: ''

# Handles None
result4 = capitalize(None)
print(result4)  # Output: None

# Single character
result5 = capitalize('a')
print(result5)  # Output: 'A'

Best Practices

  • This function differs from str.capitalize() which lowercases all characters after the first one. Use this function when you want to preserve the original casing of the rest of the string.
  • The function safely handles None and empty strings without raising exceptions, making it suitable for use in data processing pipelines where input may be inconsistent.
  • For single-character strings, this function is equivalent to str.upper().
  • Consider using str.capitalize() instead if you want to lowercase all characters except the first one.
  • This function does not handle multi-line strings specially - it only capitalizes the very first character of the entire string.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function capitalize_unicode_name 68.2% similar

    Transforms Unicode character name strings by removing the word 'capital' and capitalizing the following word, converting strings like 'capital delta' to 'Delta'.

    From: /tf/active/vicechatdev/patches/util.py
  • function clean_text 40.7% similar

    Cleans and normalizes text content by removing HTML tags, normalizing whitespace, and stripping markdown formatting elements.

    From: /tf/active/vicechatdev/improved_convert_disclosures_to_table.py
  • function int_to_alpha 39.0% similar

    Converts a non-negative integer to an Excel-style alphabetic column label (A, B, C, ..., Z, AA, AB, ..., ZZ, AAA, etc.).

    From: /tf/active/vicechatdev/patches/util.py
  • function get_path 37.7% similar

    Extracts and sanitizes a hierarchical path from a Labelled object or a tuple containing an existing path and a Labelled object, returning a tuple of capitalized, sanitized path components.

    From: /tf/active/vicechatdev/patches/util.py
  • function tree_attribute 37.2% similar

    A predicate function that determines if an identifier string represents a custom attribute in AttrTrees by checking if it starts with a capitalized character or underscore.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse