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

PythonPandasServer Side ProgrammingProgramming

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
raja
Published on 12-Feb-2021 11:20:23
Advertisements