Python Forum
Changing a character in a string
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
Reply
#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'
>>>
Reply
#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")
Reply
#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
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual Env changing mysql connection string in python Fredesetes 0 320 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,538 Jul-05-2022, 11:29 PM
Last Post: deanhystad
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,220 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Regex: a string does not starts and ends with the same character Melcu54 5 2,367 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 3,290 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,105 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Remove from end of string up to and including some character lbtdne 2 2,284 May-17-2020, 09:24 AM
Last Post: menator01
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,673 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  How to get the index of a character from a string chihaya 1 2,131 Dec-03-2019, 12:54 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020