Post Image

Python String Manipulation Camel Case a String

I recently had the itch to do a string manipulation exercise to code a function that takes a string as an input and turns it into camel case. If you are unfamiliar with camel case it is where all spaces in a string are removed and each word is a capital letter (ex. ThisIsACamelCasedString). While this is not something very practical I think this is a good exercise for beginners.

 

Develop String Manipulation

Because this is a fairly basic I think this is something that is good to develop and test right in the Python console. 

First we define our string

>>> s = 'this is my string to camel casify re@dy1'

I have letters, numbers, and special characters so I want to be able to handle those accordingly.

 

Turn String into Raw Camel Case

In order to take a string with letters of an unknown case and turn it into camel case we need to do a few things:

  1. Split the string into sub strings containing each word
  2. Lowercase all the letters in that string and capitalize the first letter
  3. Join the words of the string back together without spaces

Steps 1 and 2 can be done in a list comprehension, if we execute the strings split() method it will make a list of just the words, then if we run the capitalize() method on each of those words it will make all characters lowercase except for the first. 

That list comprehension looks like this

>>> [x.capitalize() for x in s.split()]
['This', 'Is', 'A', 'String', 'To', 'Camel', 'Casify', 'Re@dy1']

And now if we throw that list comprehension inside of an empty strings join() method it will combine all of those words into a single string separated by nothing.

>>> camel_cased = ''.join([x.capitalize() for x in s.split()])
>>> camel_cased
>>> 'ThisIsMyStringToCamelCasifyRe@dy1'

So at this point we have a camel cased string, however I would also like to be able to remove numbers and special characters as specified so lets develop that logic

 

Removing Numbers

In order to remove numbers from a string we can again use a list comprehension but this time it can be more basic. Instead of splitting the string up into words we can just iterate over each character that is not numeric. 

Below is the list comprehension to do that

>>> [x for x in camel_cased if not x.isnumeric()]
['T', 'h', 'i', 's', 'I', 's', 'A', 'S', 't', 'r', 'i', ~ Output Truncated ~]

So as you can see we now have a list of each character that made up our string, so if we pass that into an empty strings join method again it will be properly formatted.

>>> camel_cased = ''.join([x for x in camel_cased if not x.isnumeric()])
>>> camel_cased
>>> 'ThisIsMyStringToCamelCasifyRe@dy'

Now our string is devoid of all numbers

 

Removing Special Characters

And lastly to remove all special characters we do another list comprehension nearly identical to the one above except this time we iterate on every character that is alphanumeric which means either an alphabetical character or number. 

>>> camel_cased = ''.join([x for x in camel_cased if x.isalnum()])
>>> camel_cased
>>> 'ThisIsMyStringToCamelCasifyRedy'

Now our string is devoid of all special characters.

 

Putting it All Together

At this point we have the logic to camel case a string, remove numbers, and remove special characters so now we can stuff all of that into a function and build some conditional logic around it.

def camel_casify(s, preserve_nums=True, preserve_special_chars=True):
    camel_cased = ''.join([x.capitalize() for x in s.split()])
    if not preserve_nums:
        camel_cased = ''.join([x for x in camel_cased if not x.isnumeric()])
    if not preserve_special_chars:
        camel_cased = ''.join([x for x in camel_cased if x.isalnum()])
    return camel_cased

As you can see I have defined a function that takes a string as the main argument, and then 2 boolean arguments to preserve numbers and preserve special characters which by default are set to True.

The conditional logic in this function is based on the 2 boolean variables, if one is set to false, it strips out the respective character type.

 

If we run this function we get the following output.

>>> camel_casify('this is a string to camel casify re@dy1')
'ThisIsAStringToCamelCasifyRe@dy1'
>>> camel_casify('this is a string to camel casify re@dy1', preserve_nums=False)
'ThisIsAStringToCamelCasifyRe@dy'
>>> camel_casify('this is a string to camel casify re@dy1', preserve_nums=False, preserve_special_chars=False)
'ThisIsAStringToCamelCasifyRedy'

 

While this particular function is not super useful in a practical sense I thought it was a good exercise for beginners to work with string manipulation. If you have any questions please leave a comment below

 

 



Comments (0)
Leave a Comment