Verify Camel Case String and Split in Python



The result for splitting camel case strings into series as,

enter the sring:
pandasSeriesDataFrame
Series is:
0    pandas
1    Series
2    Data
3    Frame
dtype: object

To solve this, we will follow the steps given below −

Solution

  • Define a function that accepts the input string

  • Set result variable with the condition as input is not lowercase and uppercase and no ’_’ in input string. It is defined below,

result = (s != s.lower() and s != s.upper() and "_" not in s)
  • Set if condition to check if the result is true the apply re.findall method to find camel case pattern and convert input string into series. It is defined below,

pd.Series(re.findall(r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', s)
  • If the condition becomes false, then print the input is not in a camel case format.

Example

Now, let’s check its implementation to get a better understanding −

import pandas as pd
import re
def camelCase(s):
   result = (s != s.lower() and s != s.upper() and "_" not in s)
   if(result==True):
      series = pd.Series(re.findall(r'[A-Za-z](?:[a-z]+|[A-Z]*(?=[AZ]|$))', s))
      print(series)
   else:
      print("input is not in came case format")
s = input("enter the sring")
camelCase(s)

Output

enter the sring:
pandasSeriesDataFrame
Series is:
0    pandas
1    Series
2    Data
3    Frame
dtype: object
enter the sring: pandasseries
input is not in came case format
Updated on: 2021-02-25T07:26:35+05:30

778 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements