Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Python 🐍 challenge_30⚔️
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

Python 🐍 challenge_30⚔️

Convert string to camel case

  • Complete the method/function so that it converts dash/underscore Delimited words into camel casing.
  • The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).

Examples:

    "the-stealth-warrior" gets converted -> "theStealthWarrior"    "The_Stealth_Warrior" gets converted -> "TheStealthWarrior"
Enter fullscreen modeExit fullscreen mode
Task URL:Link

My Solution:

def to_camel_case(text):    if len(text) > 1:        if not text[0].isupper():            case_one = ''.join(x for x in text.title() if x.isalnum())            return case_one[0].lower() + case_one[1:]        elif text[0].isupper():            case_tow = ''.join(x for x in text.title() if x.isalnum())            return case_tow    else:        return ''
Enter fullscreen modeExit fullscreen mode

Code Snapshot:

Image description

Learn Python

Python top free courses from Coursera🐍💯🚀

🎥

Connect with Me 😊

🔗 Links

linkedin

twitter

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
leonardobispo profile image
Leonardo Bispo
  • Joined

same but much more simple

def to_camel_case(text: str) -> str:    return text[0] + ''.join(x for x in text.title()[1:] if x.isalnum) if text else ''
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
claudiogi profile image
ClaudioGi
  • Joined
• Edited on• Edited

This does not work as expected, but this does:

defcamelCaseOf(text):returntext[0]+''.join(cforcintext.title()[1:]ifcnotin["_","-"])print(camelCaseOf("turn_to_camel_case"))print(camelCaseOf("change-to-camel-case"))print(camelCaseOf("switch-to_camel-case"))
Enter fullscreen modeExit fullscreen mode

output:

turnToCamelCasechangeToCamelCaseswitchToCamelCase
Enter fullscreen modeExit fullscreen mode

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software🌟developer👨‍💻
  • Location
    Egypt/Alex
  • Education
    HarvardX & Udacity
  • Work
    Software developer
  • Joined

More fromMahmoud EL-kariouny

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp