Python Forum
Changing a character in a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Changing a character in a string (/thread-16740.html)



Changing a character in a string - Livne_ye - Mar-12-2019

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


RE: Changing a character in a string - Larz60+ - Mar-12-2019

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'
>>>



RE: Changing a character in a string - hshivaraj - Mar-12-2019

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")



RE: Changing a character in a string - Livne_ye - Mar-13-2019

(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


RE: Changing a character in a string - Larz60+ - Mar-13-2019

there are two that I think are outstanding:
  1. http://openbookproject.net/thinkcs/python/english3e/
  2. https://www.python-course.eu/python3_course.php