Python Forum
Removing string within string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing string within string
#1
I am trying to remove part of a string withing a string. for example if a string is "atgtga" i want the output to be "atg" my if statement is
[inline]'if (x=="tga" or x=="tag" or x=="taa"):
do something
[/inline]

i tried using the .replace("tga","") method but my TA was saying that it only masks that part of the string instead of removing it.

i tried a new approach but i am still stuck on the if statement.

[inline]x="atgtgaacttaa"
c=0
q=3
while(q<=len(x)):
x=x[c:q]

if(x=="tga" or x=="taa" or x=="tag"):

c=c+3
q=q+3
print(x)[/inline]
Reply
#2
I don't know what you're TA is talking about. The replace method replaces. They called the replace method replace because it replaces. Replacing is what the replace method does, so they called it replace. Why would you call the replace method replace if it didn't replace? If the replace method didn't replace, it wouldn't be called replace.

Sorry, Mojo Jojo attack.

Anyway, the only issue I can think of with the replace method that replaces is that it doesn't replace in place. It returns a new string with the replacement replaced, so you need to assign it to something:

x.replace('tga', '') # x does not change
x = x.replace('tga', '') # x is replaced with a string where the replacement is done.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
learn to tag python code properly on this forum.  if you don't know how, then ask in the Board section before doing any more posting.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  string to float conversion fails PetarPetrenko 10 945 Feb-29-2024, 04:20 PM
Last Post: deanhystad
  Help with extracting characters from string valentino1337 2 435 Feb-19-2024, 01:17 PM
Last Post: Pedroski55
  String to Number in Python Godjuned 3 1,345 Nov-17-2022, 07:22 AM
Last Post: Godjuned
  number to string Ali_ 1 1,269 Mar-31-2022, 11:22 AM
Last Post: ndc85430
  position of shortest string Ali_ 2 1,467 Mar-17-2022, 03:48 PM
Last Post: DeaD_EyE
  Count occurences in a string and add to a list using loops Leyo 4 1,668 Mar-11-2022, 03:52 PM
Last Post: Leyo
  Finding how many times substring is in a string using re module ranbarr 4 2,944 May-21-2021, 06:14 PM
Last Post: nilamo
  Checking if string starts the same but end differently using re module ranbarr 1 1,669 May-20-2021, 06:23 PM
Last Post: Gribouillis
  I need help writing this code for string properties and methods hannah71605 4 3,099 Mar-22-2021, 12:36 PM
Last Post: menator01
  Searching for parts of a string or word Ulumulu 8 3,712 Mar-15-2021, 07:02 PM
Last Post: Ulumulu

Forum Jump:

User Panel Messages

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