views:

108

answers:

4

I am having a text like

s = bluesky

i want to get it as s = ******* (equal no of * as no of characters)

I am searching for a regular expression for python. Plz help. Any help will be appreciated

Edit 1 :

b = '*'*len(s)

How can we do it in Django Template

+6  A: 

You don't need a regex for this:

s = 'bluesky'
b = '*'*len(s)
print b

output :

>>> s = 'bluesky'
>>> b = '*'*len(s)
>>> print b
*******
pyfunc
unless that `s = ` bit is part of the string.
Mark
@Mark: Looks like it is not from the example that he has given. This is a typical newbie question that comes from some one with C/C++ background. Doing this in C/C++ would have been a small exercise. Simple question like this tells how python rocks!
pyfunc
I'm pretty sure that @Rajasekar can actually assign values, which is what your answer effectively tells him to do. I'm pretty sure that he is looking for `string.replace()` while thinking he needs `re.subn()` to do the job...
Kimvais
@Kimvais : I would have added that as solution in my reply too. And since you have already done that, I left my reply as is. All answers together form a response.
pyfunc
+2  A: 

No need for regexp, just text.replace('bluesky','*'*len('bluesky'))

e.g:

>>> text = "s = bluesky"
>>> text.replace('bluesky','*'*len('bluesky'))
's = *******'
Kimvais
A: 
>>> import re
>>> re.sub("(\w+)\s*=\s*(\w+)", lambda m: "%s = %s" % (m.group(1), '*'*len(m.group(2))), "s = bluesky")
's = *******'
>>> re.sub("(\w+)\s*=\s*(\w+)", lambda m: "%s = %s" % (m.group(1), '*'*len(m.group(2))), "cat=dog")
'cat = ***'

Assuming your string is literally s = bluesky

Mark
+2  A: 

How can we do it in Django Template

In a Django template? Dead easy.

{% for char in s %}*{% endfor %}

Where s is the template variable whose value is bluesky.

Manoj Govindan