473,385 Members | 1,311 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,385 software developers and data experts.

How to change an element of a string?

Hi,

in an application I'm developing I need to change some elements in a string. It contains just 1 and 0, and I need to change, sometimes, a particular 1 to 0 or viceversa.

Is there any simple method to do this?

Thanks,
Carlo
Nov 22 '07 #1
4 2327
bvdet
2,851 Expert Mod 2GB
Hi,

in an application I'm developing I need to change some elements in a string. It contains just 1 and 0, and I need to change, sometimes, a particular 1 to 0 or viceversa.

Is there any simple method to do this?

Thanks,
Carlo
There are a couple of ways you can approach this.
Expand|Select|Wrap|Line Numbers
  1. >>> s = '100111010100011'
  2. >>> s1 = s.replace('111', '101')
  3. >>> s1
  4. '100101010100011'
  5. >>> s2 = s1[:4]+'1'+s1[5:]
  6. >>> s2
  7. '100111010100011'
  8. >>> 
The string replace() method and slicing. The string itself is immutable, so these methods return a new string.
Nov 23 '07 #2
The string replace() method and slicing. The string itself is immutable, so these methods return a new string.
Mmm, maybe I can use the slicing to change just one element.
Nov 23 '07 #3
bartonc
6,596 Expert 4TB
Mmm, maybe I can use the slicing to change just one element.
Yep. Like lines 5 thru 7 above do...
Nov 23 '07 #4
Yep. Like lines 5 thru 7 above do...
Yes, of course :D
Thanks.
Nov 23 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: magnolio | last post by:
THE PROBLEM ************************************************************** i need to send a xml content in a web service, but i use latin american charset. HOW TO CHANGE THE CHARSET?? MY CODE:...
2
by: Isz | last post by:
Hi Group: I would like to know if it is possible to change the name of an attribute to something else. My setup is like this: I have serveral SQL tables that I nest and join so that it all...
2
by: Daniel Lidström | last post by:
Hi, I would like to know the cleanest way to change the serialization of my Line class from: <Line staStart="2327.02" length="10.00000003390744"> <End>549016.570965 57945.741122</End>...
2
by: Philipp | last post by:
Hi, I wrote a small c# programm, which generates an xml-string by serializing a class and sends it to a server. Now I want to change some values in that xml-string. The only way for me to do...
15
by: Sunny | last post by:
Hi, I can change the lement opacity in IE using. abc.style.filter = 'alpha(opacity=' + 10 + ')'; But this dont work in firefox, In firefox it throws error. How I can change the opacity of an...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.