Sai A Sai A
Updated date Aug 14, 2023
In this blog, we will learn various methods to effortlessly convert lists of strings to StudlyCase (UpperCamelCase) capitalization in Python.

Introduction:

String manipulation is a cornerstone of programming, and one common task is converting text to various capitalization styles. In this blog, we'll focus on StudlyCase (also known as UpperCamelCase) and explore multiple methods to convert a list of strings to this style using Python. StudlyCase involves capitalizing the first letter of each word, without spaces or punctuation. 

Method 1: Using a Loop

def to_studly_case(word):
    return word.capitalize()

def list_to_studly_case_method1(string_list):
    studly_list = []
    for string in string_list:
        words = string.split()
        studly_words = [to_studly_case(word) for word in words]
        studly_string = ''.join(studly_words)
        studly_list.append(studly_string)
    return studly_list

Output:

Input: ["hello world", "python programming"]
Output: ["HelloWorld", "PythonProgramming"]

This method employs a loop to iterate through each string in the input list. For each string, we split it into words and apply the to_studly_case function, which capitalizes the first letter of each word. We then join the capitalized words to create the StudlyCase string.

Method 2: Using str.title()

def list_to_studly_case_method2(string_list):
    studly_list = []
    for string in string_list:
        studly_string = ''.join(word.title() for word in string.split())
        studly_list.append(studly_string)
    return studly_list

Output:

Input: ["hello world", "python programming"]
Output: ["HelloWorld", "PythonProgramming"]

Here, we use the str.title() method to capitalize the first letter of each word in a string. We iterate through the input list, split each string into words, apply the title() method to each word, and join them back together.

Method 3: Using Regular Expressions

import re

def list_to_studly_case_method3(string_list):
    studly_list = []
    for string in string_list:
        words = re.findall(r'\w+', string)
        studly_words = [word.capitalize() for word in words]
        studly_string = ''.join(studly_words)
        studly_list.append(studly_string)
    return studly_list

Output: 

Input: ["hello world", "python programming"]
Output: ["HelloWorld", "PythonProgramming"]

In this approach, we employ regular expressions (re.findall()) to extract words from each string. We then capitalize each word and join them to form the StudlyCase string.

Method 4: Using str.capitalize() and str.join()

def list_to_studly_case_method4(string_list):
    studly_list = []
    for string in string_list:
        words = string.split()
        studly_words = [word.capitalize() for word in words]
        studly_string = ''.join(studly_words)
        studly_list.append(studly_string)
    return studly_list

Output:

Input: ["hello world", "python programming"]
Output: ["HelloWorld", "PythonProgramming"]

Similar to Method 1, we split each string into words, capitalize each word, and then join them back together using str.join().

Conclusion:

In this blog, we explored the four distinct methods for converting a list of strings to StudlyCase in Python. Whether you opt for a loop-based approach, utilize built-in methods like str.title(), harness the power of regular expressions, or combine string manipulation functions.

Comments (0)

There are no comments. Be the first to comment!!!