How to Slugify Text in Python 


               


How to Slugify Text in Python



Python


In this article, we show how to slugify a string in Python.

By slugifying a string, what is meant is adding a slug (-) in each of the string where there is a space.

So, for example, if we have the string, "How to build a website in Python", this string slugified would be, "How-to-build-a-website-in-Python"

Slugifying a string is needed for things such as inputting a title into a database. Instead of having spaces which could be less desirable, we just slugify the text. Many times, this is just standard practice for cdertain types of data.

So how do we slugify text in Python.

We can use the following code shown below.



So all we have to do is import slugify from django.utils.text.

We then create a variable called title and assign it to "How to move to Texas"

We then slugify the text through the slugify function and save this slugified text in the slugtext variable.

We then show the contents of slug text, which outputs, 'How-to-move-to-Texas'

And this is all that is necessary to slugify text in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...