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
This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 2 years ago.
I'm a newbie in python.
Basically what I wanted to achieve is:
I have a block of sample data as shown here:
384&cp54,cp170,cp285,cp401,cp517,cp633,cp748,cp864,cp980,cp1096,cp1205,cp1315,cp1424,cp1534,cp1643,cp1753,cp1862,cp1972,cp2082,cp2191,cp2301,cp2410,cp2520,cp2630,cp2739,cp2849,cp2958,cp3068,cp3178,cp3287,cp3342,cp3397,cp3451,cp3506,cp3561,cp3616,cp3671,cp3725,cp3780,cp3835&hp21,hp37,hp49,hp58,hp66,hp73,hp79,hp85,hp91,hp96,hp101,hp105,hp109,hp113,hp117,hp121,hp125,hp129,hp132,hp136,hp139,hp142,hp146,hp149,hp152,hp155,hp158,hp161,hp164,hp166,hp168-170,hp172-174,hp176-178,hp180
How do I code using python3 to achieve the following without having to manually to add the " " sign for each item after the comma.
The desired outcome:
datalist = list()
print(datalist)
Results:
['stritem1','stritem2','stritem3','stritem4'....etc]
Something like this should work:
rawData = "384&cp54,cp170,cp285,cp401,cp517,cp633,cp748,cp864,cp980,cp1096,cp1205,cp1315,cp1424,cp1534,cp1643,cp1753,cp1862,cp1972,cp2082,cp2191,cp2301,cp2410,cp2520,cp2630,cp2739,cp2849,cp2958,cp3068,cp3178,cp3287,cp3342,cp3397,cp3451,cp3506,cp3561,cp3616,cp3671,cp3725,cp3780,cp3835&hp21,hp37,hp49,hp58,hp66,hp73,hp79,hp85,hp91,hp96,hp101,hp105,hp109,hp113,hp117,hp121,hp125,hp129,hp132,hp136,hp139,hp142,hp146,hp149,hp152,hp155,hp158,hp161,hp164,hp166,hp168-170,hp172-174,hp176-178,hp180"
datalist = rawData.split(',')
print(datalist)
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:
Remove 'u' from a python list
(5 answers)
Closed 5 years ago.
i saw that this question already been asked but the solutions i saw didn't worked for me.
i have a python script with tuple list which i want to retrieve without the unicode char (u') and without any other chars (like < [] > or < ' >) so only the data will pass to parameter.
my code look like this -
sql_cursor = con.cursor()
cursor = con.cursor()
cursor.execute(get_alerted_ip)
Results = cursor.fetchall()
for row in Results:
ip_to_open.append(row)
print (row)
con.close()
the output is -
(u'172.1.1.124',)
and the wanted output should look like -
172.1.1.124
I have already tried to convert the tuple to list or string in order to use methods like replace but it's not working.
what am i doing wrong? please help.
Simply
row[0].encode("utf-8")
will return the ascii version of the string
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.