Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-21-2005, 04:51 PM   #1
LQ Newbie
Registered: Nov 2003
Location: Edmonton, Alberta, Canada
Distribution: Slackware 9.1
Posts: 23

Rep: Reputation: 15
Python search and replace

I'm trying to use python to do a simple search and replace within STAX ( My code is:

myVar = "GrunthosTheFlatulent"

import re
re.sub('u', 'Q', myVar)

<call function="'STAXUtilLogAndMsg'">
'myVar is now %s' %(myVar)

I expected to get GrQnthosTheFlatQlent but got GrunthosTheFlatulent instead. Can someone tell me what I'm doing wrong? Thanks!
Old 02-22-2005, 07:54 PM   #2
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
strings are immutable (you can't change them). You have to say myVar = re.sub('u', 'Q', myVar). The difference is that this make myVar refer to a new object (in terms of C, it changes the value of the pointer), whereas mutating methods (such as foo.append(bar)) changes the contents of the object that foo refers to (the object that the pointer points at).

Hope this helps,



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
global search and replace? jkcunningham Linux - General 5 03-15-2006 02:43 PM
search/replace in many files allelopath Linux - General 1 08-02-2005 09:21 PM
search and replace C code vexer Programming 3 05-18-2004 11:11 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 09:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration