473,383 Members | 1,863 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,383 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 2326
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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
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?
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.