Nest all content into snippets
This commit is contained in:
32
snippets/python/s/snake.md
Normal file
32
snippets/python/s/snake.md
Normal file
@ -0,0 +1,32 @@
|
||||
---
|
||||
title: Snakecase string
|
||||
type: snippet
|
||||
language: python
|
||||
tags: [string,regexp]
|
||||
cover: organizer
|
||||
dateModified: 2020-11-02T19:28:35+02:00
|
||||
---
|
||||
|
||||
Converts a string to snake case.
|
||||
|
||||
- Use `re.sub()` to match all words in the string, `str.lower()` to lowercase them.
|
||||
- Use `re.sub()` to replace any `-` characters with spaces.
|
||||
- Finally, use `str.join()` to combine all words using `-` as the separator.
|
||||
|
||||
```py
|
||||
from re import sub
|
||||
|
||||
def snake(s):
|
||||
return '_'.join(
|
||||
sub('([A-Z][a-z]+)', r' \1',
|
||||
sub('([A-Z]+)', r' \1',
|
||||
s.replace('-', ' '))).split()).lower()
|
||||
```
|
||||
|
||||
```py
|
||||
snake('camelCase') # 'camel_case'
|
||||
snake('some text') # 'some_text'
|
||||
snake('some-mixed_string With spaces_underscores-and-hyphens')
|
||||
# 'some_mixed_string_with_spaces_underscores_and_hyphens'
|
||||
snake('AllThe-small Things') # 'all_the_small_things'
|
||||
```
|
||||
Reference in New Issue
Block a user