473,405 Members | 2,154 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,405 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 46811
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.