Write a program in Python to verify camel case string from the user, split camel cases, and store them in a new series

Camel case is a naming convention where the first letter is lowercase and each subsequent word starts with an uppercase letter (e.g., "pandasSeriesDataFrame"). This tutorial shows how to verify if a string is in camel case format and split it into a pandas Series.

Understanding Camel Case Validation

A valid camel case string must satisfy these conditions:

  • Not all lowercase
  • Not all uppercase
  • Contains no underscores

Solution Steps

To solve this problem, we follow these steps:

  • Define a function that accepts the input string
  • Check if the string is in camel case format
  • Use regex to split camel case words
  • Convert the result into a pandas Series

Complete Implementation

import pandas as pd
import re

def camelCase(s):
    # Check if string is in camel case format
    result = (s != s.lower() and s != s.upper() and "_" not in s)
    
    if result == True:
        # Split camel case using regex pattern
        words = re.findall(r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', s)
        series = pd.Series(words)
        print("Series is:")
        print(series)
    else:
        print("Input is not in camel case format")

# Test with valid camel case string
print("Testing with valid camel case:")
camelCase("pandasSeriesDataFrame")

print("\nTesting with invalid camel case:")
camelCase("pandasseries")
Testing with valid camel case:
Series is:
0    pandas
1    Series
2      Data
3     Frame
dtype: object

Testing with invalid camel case:
Input is not in camel case format

How the Regex Pattern Works

The regex pattern r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))' works as follows:

  • [A-Za-z] - Matches the first letter of each word
  • (?:[a-z]+|[A-Z]*(?=[A-Z]|$)) - Non-capturing group that matches either lowercase letters or uppercase letters followed by another uppercase letter or end of string

Testing Different Cases

import pandas as pd
import re

def camelCase(s):
    result = (s != s.lower() and s != s.upper() and "_" not in s)
    
    if result == True:
        words = re.findall(r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', s)
        series = pd.Series(words)
        return series
    else:
        return "Not camel case format"

# Test various strings
test_cases = ["camelCaseString", "HTMLParser", "allowercase", "ALLUPPERCASE", "snake_case"]

for test in test_cases:
    print(f"Input: {test}")
    result = camelCase(test)
    print(f"Result: {result}")
    print("-" * 30)
Input: camelCaseString
Result: 0     camel
1      Case
2    String
dtype: object
------------------------------
Input: HTMLParser
Result: 0      HTML
1    Parser
dtype: object
------------------------------
Input: allowercase
Result: Not camel case format
------------------------------
Input: ALLUPPERCASE
Result: Not camel case format
------------------------------
Input: snake_case
Result: Not camel case format
------------------------------

Conclusion

This solution effectively validates camel case strings and splits them into individual words stored in a pandas Series. The regex pattern handles both simple camel case and complex cases with consecutive uppercase letters like "HTMLParser".

Updated on: 2026-03-25T16:35:46+05:30

897 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements