Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Changing a character in a string
#1
Hello fellas! first time here.
i have the following string:
"ATCGATCGATCGATCGACTGACTAGTCATAGCTATGCATGTAGCTACTCGATCGATCGATCGACGATCGATATCGATGCATCGACTACTAT"

i want to write a command which will replace all of the "T" characters into "U" characters, and which the new "U" characters will be in the exact same location as the old "T" characters were.

like so:

"AUCGAUCGAUCGAUCGACUGACUAGUCAUAGCUAUGCAUGUAGCUACUCGAUCGAUCGAUCGACGAUCGAUAUCGAUGCAUCGACUACUAU"

do you have any idea how can i write such a command?

Thanks and have a great day
Quote
#2
looks like a genetic sequence
What have you tried?
We're glad to help, but don't generally write the code for you.
But since this is so simple:
>>> zz = "ATCGATCGATCGATCGACTGACTAGTCATAGCTATGCATGTAGCTACTCGATCGATCGATCGACGATCGATATCGATGCATCGACTACTAT"
>>> zz = zz.replace('T', 'U')
>>> zz
'AUCGAUCGAUCGAUCGACUGACUAGUCAUAGCUAUGCAUGUAGCUACUCGAUCGAUCGAUCGACGAUCGAUAUCGAUGCAUCGACUACUAU'
>>>
Quote
#3
This is an alternate solution for str.replace which I personal would not use for replacing char's, just because from a performance perspective. Despite for the record here's how you would do it.

import re
re.sub('T', 'U', "ATCGATCGATCGATCGACTGACTAGTCATAGCTATGCATGTAGCTACTCGATCGATCGATCGACGATCGATATCGATGCATCGACTACTAT")
Quote
#4
(Mar-12-2019, 09:54 PM)Larz60+ Wrote: looks like a genetic sequence
What have you tried?
We're glad to help, but don't generally write the code for you.
But since this is so simple:
>>> zz = "ATCGATCGATCGATCGACTGACTAGTCATAGCTATGCATGTAGCTACTCGATCGATCGATCGACGATCGATATCGATGCATCGACTACTAT"
>>> zz = zz.replace('T', 'U')
>>> zz
'AUCGAUCGAUCGAUCGACUGACUAGUCAUAGCUAUGCAUGUAGCUACUCGAUCGAUCGAUCGACGAUCGAUAUCGAUGCAUCGACUACUAU'
>>>

Thank you so much.

i'm only a begginer. i've tried to look for a python command in several websites and python tutorial, bot to no avail.
do you have any recommendation for tutorials (video or non video - does not matter) which may lead me into a good path?

have a good day buddy
appreciate the help
Quote
#5
there are two that I think are outstanding:
  1. http://openbookproject.net/thinkcs/python/english3e/
  2. https://www.python-course.eu/python3_course.php
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 204 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  How to get the index of a character from a string chihaya 1 176 Dec-03-2019, 12:54 PM
Last Post: buran
  python gives wrong string length and wrong character thienson30 2 281 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Find string and add character - newbi PyDK 1 310 May-15-2019, 01:22 PM
Last Post: ichabod801
  remove string character from url jacklee26 10 1,310 Mar-25-2019, 03:56 PM
Last Post: Larz60+
  Replace changing string including uppercase character with lowercase character silfer 11 1,105 Mar-25-2019, 12:54 PM
Last Post: silfer
  [SOLVED] Changing a string to an int Panda 3 1,193 Jun-23-2018, 08:12 PM
Last Post: gontajones
  How Do I find Index of a character in string? ilcaa72 5 945 May-23-2018, 11:44 PM
Last Post: wavic
  testing if a character in a string is an intended one Skaperen 2 829 Feb-27-2018, 02:51 AM
Last Post: Skaperen
  find a character in a string in python3 tony1812 6 3,044 Sep-27-2017, 03:28 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)