This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 1 year ago.
I write the code below but replace method is not working.
code:
courses = input("Please enter the courses you have taken previously with letter grades: ")
courses.replace("M","X")
print(courses)
Please enter the courses you have taken previously with letter grades:
MATH101:A;SPS101:B;CS201:B+;HIST191:D;CS204:F;CS210:S:
MATH101:A;SPS101:B;CS201:B+;HIST191:D;CS204:F;CS210:S:
replace won't mutate the string in place. str.replace()
courses = courses.replace("M", "X")
The replace method doesn't replace the text in original string, it returns a new one.
What you need to do is -
courses = courses.replace("M", "X")
print(courses)
courses = courses.replace("M","X")
Just like 12944qwerty says, You need to reassign into courses
Related
This question already has answers here:
How to remove specific substrings from a set of strings in Python? [duplicate]
(9 answers)
Closed 1 year ago.
I'm using Python to get title names for a header in a program.
Each title comes like: "OXD/Overview Controls", "OXD/BULK/BULK Details Controls" and more...
Currently I am using a value/split() method to remove the "/" returning only "OXD Overview Controls" and "OXD BULK", however I would like to remove the "OXD" and "Controls" portions of the string. Any help is appreciated, my current code is listed below.
if value <> None:
value = value.split("/")[:2]
value = ' '.join(value)
else:
value = ""
return value.upper()
#Steven Barnard and #DSteman: Sorry, I wrote something and you both was faster as me. ^^'
You can use the string_variable.replace(search,replace_with) function.
Replace OXD with nothing:
value.replace("OXD","")
Replace / with Space:
value.replace("/"," ")
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
To sum up, I need to ask a question:
If the user doesn't introduce a str, I need the question to be asked repeatly until the use enter a str.
This is what I got so far.
x = input('What is your name? ')
if x != str
x=input('Sorry, you need to introduce a str. What is your name? ')
I believe what you mean is that if the user doesn't input all letters, the program will keep asking for the user's name. You can use the method isalpha() for that:
x = input('What is your name? ')
while not [s.isalpha() for s in x.split()] == [True for s in x.split()]:
x = input('Sorry, you need to introduce a str. What is your name? ')
This question already has answers here:
How to print a string at a fixed width?
(7 answers)
Closed 2 years ago.
I have created a program.Here I need to leave some space and print an String after the spaces.I did that program with format in python.my code is below,
name = "myname"
print("{0:>10}".format(name))
#output=" myname"
Here, the sum of empty spaces and the length of name is equals to 10.I have declared the size inside print.
But I need to declare the size as a variable.I tried it like this,
num = 10
name = "myname"
print("{0:>num}".format(name))
but it did not worked.I need to know how I can fix this.I need to take the same output with giving the size with an variable.please help...
Try this one:
num_of_spaces = 10
name = "myname"
name.rjust(num_of_spaces)
Try:
num = 10
name ="myname"
print(" "*num,name)
Or you can also do it via:
name ="myname"
print('{0} {1}'.format(' '*num,name))
This question already has answers here:
How to retrieve a variable's name in python at runtime?
(9 answers)
Closed 9 years ago.
I've designed a code that calculates the percentage of 'CG' appears in a string; GC_content(DNA).
Now I can use this code so that it prints the the value of the string with the highest GC-content;
print (max((GC_content(DNA1)),(GC_content(DNA2)),(GC_content(DNA3)))).
Now how would I get to print the variable name of this max GC_content?
You can get the max of some tuples:
max_content, max_name = max(
(GC_content(DNA1), "DNA1"),
(GC_content(DNA2), "DNA2"),
(GC_content(DNA3), "DNA3")
)
print(max_name)
If you have many DNA variables you could place them in a list
DNA_list = [DNA1, DNA2, DNA3]
I would coerce them into a dictionary to associate the name with the raw data and result.
DNA_dict = dict([("DNA%i" % i, {'data': DNA, 'GC': GC_Content(DNA)}) for i, DNA in enumerate(DNA_list)])
Then list comprehension to get the data you want
name = max([(DNA_dict[key]['GC'], key) for key in DNA_dict])[1]
This has the benefit of allowing variable list length
You seem to want
max([DNA1, DNA2, DNA3], key=GC_content)
It's not what you asked for but it seems to be what you need.
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 months ago.
I need to create a code where the user can input a certain number of courses, and then it will take the gpa of them, but how can I change the variable name in the loop?
I have this so far
number_courses= float(input ("Insert the number of courses here:"))
while number_courses>0:
mark_1= input("Insert the letter grade for the first course here: ")
if mark_1=="A+" :
mark_1=4.0
number_courses= number_courses-1
If I want to change the variable name of mark_one to something different each time I go through the loop, what is the simplest way I can do this? And also is it possible to change it in my input statement to ask for first, second, third... as I go through the loop? I have tried searching on google, but none of the answers I can understand as their code is far behind my level or they didn't seem to answer what I needed either. Thanks.
You want to use a list or something similar to gather the input values:
number_courses=input("Insert the number of courses here: ")
marks = []
while number_courses>0:
mark = input("Insert the letter grade for the first course here: ")
if mark == "A+":
mark = 4.0
marks.append(mark)
number_courses -= 1
print marks
Use a dictionary:
number_courses= float(input ("Insert the number of courses here:"))
marks = {'A+':4, 'A':3.5, 'B+':3}
total_marks = 0
while number_courses:
mark_1= input("Insert the letter grade for the first course here: ")
if mark_1 in marks:
total_marks += marks[mark_1] #either sum them, or append them to a list
number_courses -= 1 #decrease this only if `mark_1` was found in `marks` dict