Python replace all characters in string with space
I want to replace whitespace with underscore in a string to create nice URLs. So that for example: Show
Should become
I am using Python with Django. Can this be solved using regular expressions?
Tomerikoo 16.6k15 gold badges38 silver badges54 bronze badges asked Jun 17, 2009 at 14:41
1 You don't need regular expressions. Python has a built-in string method that does what you need:
Luke Exton 3,2362 gold badges17 silver badges32 bronze badges answered Jun 17, 2009 at 14:44
rogeriopvlrogeriopvl 48.6k8 gold badges52 silver badges58 bronze badges 8 Replacing spaces is fine, but I might suggest going a little further to handle other URL-hostile characters like question marks, apostrophes, exclamation points, etc. Also note that the general consensus among SEO experts is that dashes are preferred to underscores in URLs.
vvvvv 18.9k16 gold badges43 silver badges62 bronze badges answered Jun 17, 2009 at 15:03
Kenan BanksKenan Banks 201k34 gold badges151 silver badges171 bronze badges 7 This takes into account blank characters other than space and I think it's faster than using
answered Apr 29, 2012 at 23:18
xOnecaxOneca 8039 silver badges20 bronze badges 5 Django has a 'slugify' function which does this, as well as other URL-friendly optimisations. It's hidden away in the defaultfilters module.
This isn't exactly the output you asked for, but IMO it's better for use in URLs. answered Jun 17, 2009 at 15:15
Daniel RosemanDaniel Roseman 574k61 gold badges839 silver badges852 bronze badges 6
Using the
Unless you have multiple spaces or other whitespace possibilities as above, you may just wish to use answered Jun 17, 2009 at 14:45
Jarret HardieJarret Hardie 91.8k10 gold badges130 silver badges125 bronze badges 2 use string's replace method:
answered Jun 17, 2009 at 14:45
mdirolfmdirolf 7,3912 gold badges22 silver badges15 bronze badges Surprisingly this library not mentioned yet python package named python-slugify, which does a pretty good job of slugifying:
Works like this:
answered Sep 28, 2015 at 16:01
YashYash 6,0644 gold badges35 silver badges26 bronze badges Python has a built in method on strings called replace which is used as so:
So you would use:
I had this problem a while ago and I wrote code to replace characters in a string. I have to start remembering to check the python documentation because they've got built in functions for everything. answered Jun 18, 2009 at 12:34 I'm using the following piece of code for my friendly urls:
It works fine with unicode characters as well. answered Jun 17, 2009 at 15:36
ArmandasArmandas 1,7601 gold badge17 silver badges24 bronze badges 1 You can try this instead:
Jules Dupont 6,7417 gold badges38 silver badges37 bronze badges answered May 6, 2018 at 15:28
1
if you assign this value to any variable, it will work
by default mystring wont have this
answered Jul 31, 2016 at 16:51
OP is using python, but in javascript (something to be careful of since the syntaxes are similar.
answered Jul 4, 2010 at 23:34
skilleoskilleo 2,4221 gold badge27 silver badges34 bronze badges
eyllanesc 226k18 gold badges131 silver badges199 bronze badges answered Nov 28, 2021 at 6:25
4
Match et replace space > underscore of all files in current directory answered Jun 19, 2009 at 7:30 How do you replace a character in a string with spaces in Python?The replace() method replaces the occurrences of the substring passed as first argument (in this case the space ” “) with the second argument (in this case an empty character “”).
How do you replace a string with spaces?Approach: In the given string Str, replace all occurrences of Sub with empty spaces. Remove unwanted empty spaces in start and end of the string. Print the modified string.
How do you replace all letters in a string in Python?Syntax of replace(). Syntax: string.replace(old, new, count). Parameters:. Return Value : It returns a copy of the string where all occurrences of a substring are replaced with another substring.. How do you replace blank space in Python?In Python, there are many ways you can replace blank spaces:. strip(). replace(). join() with str. split(). translate(). re. sub(). |