472,807 Members | 5,240 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

insert chars into string

hi
is there a string method to insert characters into a string?
eg
str = "abcdef"
i want to insert "#" into str so that it appears "abc#def"

currently what i did is convert it to a list, insert the character
using insert() and then join them back as string..
thanks

Mar 18 '06 #1
2 46728
Em Sex, 2006-03-17 Ã*s 17:32 -0800, ei***********@yahoo.com escreveu:
is there a string method to insert characters into a string?


As far as I know, no. Strings are immutable in Python. That said, the
following method may do what you want:

def insert(original, new, pos):
'''Inserts new inside original at pos.'''
return original[:pos] + new + original[pos:]

Mar 18 '06 #2
ei***********@yahoo.com schrieb:
hi
is there a string method to insert characters into a string?
eg
str = "abcdef"
i want to insert "#" into str so that it appears "abc#def"

currently what i did is convert it to a list, insert the character
using insert() and then join them back as string..


If you are frequently want to modify a string in such a way, that this
is the recommended way to do so. It might be that there are
mutable-string implementations out there that make that easier for you -
but in the end, it boils down to using list, so that the overhead of
concatenating strings is encountered only once, on the end when you
actually need the result.

Diez

Mar 18 '06 #3

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

Similar topics

3
by: Ken Robinson | last post by:
This one has me stumped... Let's say I have a string consisting of n words (where n > 0). I would like to insert string 'abcd' between the first and second characters of each word of the string....
7
by: Paul Gorodyansky | last post by:
Hi, Say I have a text in my TEXTAREA box - 01234567890 I want - using script - insert say "abc" in the middle. Works almost OK in Internet Explorer (with one problem) based on their example...
10
by: Ricardo | last post by:
How can I make a insert sql string to insert direct in a table(not a dataset) using the textbox.text propert??
2
by: CK | last post by:
Hi All, Theis should be easy. I need to insert a string into an exisitng string? Example var string1 = "sweet"; var string2 = "bbb"; I want to modify string1 to be "swebbbet"; I know this...
7
by: S Wheeler | last post by:
Hi all - I need some help trying to insert / append a block of xml text into an xml document. This is for a reporting app and as new data is available, I must add it to the end of the document. It...
5
by: vunet.us | last post by:
Hello, In ASP, VBScript: How can I display the first 20 characters of a long string? How can I insert some character inside of the string on every 5th character? Thank you for help.
1
by: shrinivaskv | last post by:
How to insert a string with double quotes into db tables ? example : i want to insert a string -- gloablworld "a" within " " into db table using c++.
3
by: BonBoni | last post by:
Hi everybody I need to implement a function that receives the following parameters : char *PrefixMin , char *PrefixMax , char *InputNum All the input strings max length is 25 chars. I can...
7
by: HansWernerMarschke | last post by:
Is there a special function to insert a string into another string ? For example I want to exchange the german "Umlauts" ä,ö,ü with ae,oe and ue in a string. I also wonder if there is a function...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.