473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

string methods

I am an abject newbie, so mock away (actually no-one ever does that in
this group..)

Anyway, I want to replace one character in a string, based in that
character's position in the string.

For example if I wanted to replace the 4th character in 'foobar' (the
b)with the contents of another string, newchar, what would be the
easiest way?

I know this touches on immutability etc, but I can't find string methods
to return the first 3 characters, and then the last 2 characters, which
I could concatenate with newchar to make a new string.

I know the string methods are there, but can't find it in any docs, and
just want to check the syntax, unless there is an easier way.

Thanks.
Jul 30 '05 #1
5 1593
anthonyberet wrote:
I know this touches on immutability etc, but I can't find string methods
to return the first 3 characters, and then the last 2 characters, which
I could concatenate with newchar to make a new string.

I know the string methods are there, but can't find it in any docs, and
just want to check the syntax, unless there is an easier way.


Strings [1] are sequences [2], and therefore support slicing, which is
what you are looking for.
[1] http://docs.python.org/lib/string-methods.html
[2] http://docs.python.org/lib/typesseq.html
Jul 30 '05 #2
anthonyberet wrote:
I know this touches on immutability etc, but I can't find string methods
to return the first 3 characters, and then the last 2 characters, which
I could concatenate with newchar to make a new string.


As tiissa said, you want slicing:

py> s = "foobar"
py> s[:3]
'foo'
py> s[:3] + "B" + s[4:]
'fooBar'
py>

--
Brian Beck
Adventurer of the First Order
Jul 30 '05 #3
Brian Beck wrote:
anthonyberet wrote:
I know this touches on immutability etc, but I can't find string methods
to return the first 3 characters, and then the last 2 characters, which
I could concatenate with newchar to make a new string.


As tiissa said, you want slicing:

py> s = "foobar"
py> s[:3]
'foo'
py> s[:3] + "B" + s[4:]
'fooBar'


And if that's too ugly for you and you think you need to do this
operation a lot, just define a function to do it for you based on the
index value and string that you pass in to it.

-Peter
Jul 30 '05 #4
On Sat, 30 Jul 2005 16:15:10 +0100, anthonyberet <no****@me.invalid>
declaimed the following in comp.lang.python:


For example if I wanted to replace the 4th character in 'foobar' (the
b)with the contents of another string, newchar, what would be the
easiest way?
s = "foobar"
a = "another"
n = s[:3] + a + s[4:]
n 'fooanotherar'


-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 30 '05 #5
anthonyberet wrote:
For example if I wanted to replace the 4th character in 'foobar' (the
b)with the contents of another string, newchar, what would be the
easiest way?


Depends on how your input is specified. If you know it is the b you
want to replace, you write
text="foobar"
text = text.replace("b","baz")
text 'foobazar'

There is no issue with immutability here: .replace returns a new
string object, and you assign this to the text variable (thus dropping
the reference to the string "foobar").

If you know it is the fourth character you want to replace, you
do as people have suggested:
text="foobar"
text=text[:3]+"baz"+text[4:]
text 'foobazar'

And, if you know in advance that the string is "foobar", and that
it is the fourth character, and that the replacement string is "baz",
you write
text="foobazar"


:-)

Regards,
Martin
Jul 31 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
32
by: Tubs | last post by:
Am i missing something or does the .Net Framework have a quirk in the way methods work on an object. In C++ MFC, if i have a CString and i use the format method, i format the string i am using. ...
4
by: jemptymethod | last post by:
http://htmatters.net/htm/1/2006/01/EIBTI-for-Javascript-explicit-is-better-than-implicit.cfm
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
15
by: Mark C | last post by:
I know a string is immutable, but is there any trick or any other way to destroy a string Thanks www.quiznetonline.com
7
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string?...
4
by: Daniel Nogradi | last post by:
I am probably misunderstanding some basic issue here but this behaviour is not what I would expect: Python 2.4 (#1, Mar 22 2005, 21:42:42) on linux2 Type "help", "copyright", "credits" or...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.