Nest all content into snippets
This commit is contained in:
28
snippets/python/s/slugify.md
Normal file
28
snippets/python/s/slugify.md
Normal file
@ -0,0 +1,28 @@
|
||||
---
|
||||
title: String to slug
|
||||
type: snippet
|
||||
language: python
|
||||
tags: [string,regexp]
|
||||
cover: sliced-fruits
|
||||
dateModified: 2020-10-25T12:43:20+02:00
|
||||
---
|
||||
|
||||
Converts a string to a URL-friendly slug.
|
||||
|
||||
- Use `str.lower()` and `str.strip()` to normalize the input string.
|
||||
- Use `re.sub()` to to replace spaces, dashes and underscores with `-` and remove special characters.
|
||||
|
||||
```py
|
||||
import re
|
||||
|
||||
def slugify(s):
|
||||
s = s.lower().strip()
|
||||
s = re.sub(r'[^\w\s-]', '', s)
|
||||
s = re.sub(r'[\s_-]+', '-', s)
|
||||
s = re.sub(r'^-+|-+$', '', s)
|
||||
return s
|
||||
```
|
||||
|
||||
```py
|
||||
slugify('Hello World!') # 'hello-world'
|
||||
```
|
||||
Reference in New Issue
Block a user