string methods

A

anthonyberet

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.
 
T

tiissa

anthonyberet said:
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
 
B

Brian Beck

anthonyberet said:
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>
 
P

Peter Hansen

Brian said:
anthonyberet said:
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
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

anthonyberet said:
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
'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

:)

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top