Menu

gitpiper

camel python Code Snippet in 2024

stringregexpintermediate

Last Updated: 20 April 2024

Converts a string to camelcase.

  • Use re.sub() to replace any - or _ with a space, using the regexp r"(_|-)+".
  • Use str.title() to capitalize the first letter of each word and convert the rest to lowercase.
  • Finally, use str.replace() to remove spaces between words.
from re import sub def camel(s): s = sub(r"(_|-)+", " ", s).title().replace(" ", "") return ''.join([s[0].lower(), s[1:]])
camel('some_database_field_name') # 'someDatabaseFieldName' camel('Some label that needs to be camelized') # 'someLabelThatNeedsToBeCamelized' camel('some-javascript-property') # 'someJavascriptProperty' camel('some-mixed_string with spaces_underscores-and-hyphens') # 'someMixedStringWithSpacesUnderscoresAndHyphens'

python snippet similar to camel For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️