Home > Back-end >  Python return if statement
Python return if statement

Time:08-25

Unclear on how to frame the following function correctly:

Creating a function that will take in a string and return the string in camel case without spaces (or pascal case if the first letter was already capital), removing special characters

text = "This-is_my_test_string,to-capitalize"

def to_camel_case(text):
    # Return 1st letter of text   all letters after
    return text[:1]   text.title()[1:].replace(i" ") if not i.isdigit()

# Output should be "ThisIsMyTestStringToCapitalize"

the "if" statement at the end isn't working out, and I wrote this somewhat experimentally, but with a syntax fix, could the logic work?

CodePudding user response:

Other answers don't address a camelCase example. This is a camelCase string and this is a PascalCase string.

You were quite close to the right solution, using functions title() and replace() was a good approach but the syntax is different:

from string import ascii_letters

def to_camel_case(text):
    return text[0]   ''.join([s if s in ascii_letters else ' ' for s in text]).title().replace(' ', '')[1:]

text = "This-is_my_test_string,to-capitalize"

print(to_camel_case(text))
#  >>> ThisIsMyTestStringToCapitalize

text = "this-is_my_test_string,to-capitalize"

print(to_camel_case(text))
#  >>> thisIsMyTestStringToCapitalize

CodePudding user response:

Providing the input string does not contain any spaces then you could do this:

from re import sub
def to_camel_case(text, pascal=False):
    r = sub(r'[^a-zA-Z0-9]', ' ', text).title().replace(' ', '')
    return r if pascal else r[0].lower()   r[1:]
ts = 'This-is_my_test_string,to-capitalize'
print(to_camel_case(ts, pascal=True))
print(to_camel_case(ts))

Output:

ThisIsMyTestStringToCapitalize
thisIsMyTestStringToCapitalize

CodePudding user response:

Here is a short solution using regex. First it uses title() as you did, then the regex finds non-alphanumeric-characters and removes them, and finally we take the first character to handle pascal / camel case.

import re

def to_camel_case(s):
    s1 = re.sub('[^a-zA-Z0-9] ', '', s.title())
    return s[0]   s1[1:]

text = "this-is2_my_test_string,to-capitalize"
print(to_camel_case(text)) # ThisIsMyTestStringToCapitalize

CodePudding user response:

The below should work for your example.

Splitting apart your example by anything that isn's alphanumeric or a space. Then capitalizing each word. Finally, returning the re-joined string.

import re


def to_camel_case(text):
    words = re.split(r'[^a-zA-Z0-9\s]', text)
    return "".join([word.capitalize() for word in words])

text_to_camelcase = "This-is_my_test_string,to-capitalize"
print(to_camel_case(text_to_camelcase))

CodePudding user response:

use the split function to split between anything that is not a letter or a whitespace and the function .capitalize() to capitalize single words

import re
text_to_camelcase = "This-is_my_test_string,to-capitalize"

def to_camel_case(text):
    split_text = re.split(r'[^a-zA-Z0-9\s]', text)

    cap_string = ''
    for word in split_text:
        cap_word = word.capitalize()
        cap_string  = cap_word

    return cap_string


print(to_camel_case(text_to_camelcase))
  • Related