From 4d544ad19210299f5c74640676d971b8342aae82 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 23 Aug 2019 10:13:28 +0300 Subject: [PATCH] Update camel.md --- snippets/camel.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/snippets/camel.md b/snippets/camel.md index edf9800cc..a989e8fc8 100644 --- a/snippets/camel.md +++ b/snippets/camel.md @@ -5,17 +5,14 @@ tags: string,regexp,intermediate Converts a string to camelcase. -Break the string into words and combine them capitalizing the first letter of each word, using `title`. - -`(\s|_|-)+` matches one or more spaces (`\s`), underscores (`_`) or hyphens (`-`). `re.sub` replaces these matches with single spaces. -`title` capitalizes the first letter of each word. `replace(" ", "")` removes the spaces between words. +Break the string into words and combine them capitalizing the first letter of each word, using a regexp, `title()` and `lower`. ```py import re def camel(s): - pascal = re.sub(r"(\s|_|-)+", " ", s).title().replace(" ", "") - return pascal[0].lower() + pascal[1:] + s = re.sub(r"(\s|_|-)+", " ", s).title().replace(" ", "") + return s[0].lower() + s[1:] ``` ```py