By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

Wierd problem with replace

P: n/a
Hello,
I've this code:

print "Login1:", login
login.replace("@","_")
print "Login2:", login

Which produces this output:

Login1: tester@root
Login2: tester@root

Why is Login2 still tester@root and not tester_root? I thought that I've
replaced the @ with _??

Thx,
Florian

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Florian Lindner wrote:
Hello,
I've this code:

print "Login1:", login
login.replace("@","_")
print "Login2:", login

Which produces this output:

Login1: tester@root
Login2: tester@root

Why is Login2 still tester@root and not tester_root? I thought that I've
replaced the @ with _??


Trying it at the command line might be instructive:
login = 'tester@root'
print "Login1:", login Login1: tester@root login.replace("@", "_") 'tester_root' login

'tester@root'

Strings are immutable. The call to replace() does not modify
the original string, but returns a new one. You'll see similar
behaviour with other string methods (or any methods on immutables).

-Peter
Jul 18 '05 #2

P: n/a
Florian Lindner wrote:
Hello,
I've this code:

print "Login1:", login
login.replace("@","_")
print "Login2:", login

Which produces this output:

Login1: tester@root
Login2: tester@root

Why is Login2 still tester@root and not tester_root? I thought that I've
replaced the @ with _??

Thx,
Florian


Florian,
x = "tester@root"
y = x.replace("@","_")
print y

tester_root

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.