how to format with double quotes using f-format in python?

24 Views Asked by At

I want to enclose each element of a list with double quotes instead of single?

With f-string I'm not able to get double quotes and when I try solutions from the internet it's applying double quotes to the elements inside the e list.

res.append(f'{name}[{", ".join(e)}]')

This gives the output as

['Rishi[EC1]', 'Divya[]']

but I want it as

["Rishi[EC1]", "Divya[]"]

The whole code:

s=list(input().split(" "))
res=[]
EC1=["CC1","CC2"]
EC2=["CC1","CC2"]
EC3=["CC1","CC3"]
EC4=["CC1","CC2","CC3"]
EC5=["CC2","CC5"]
EC6=["CC4","CC5"]
for i in s:
    e=[]
    stu=i.split("/")
    name=stu[0]
    courses=stu[1:-1]
    gpa=stu[-1]
    if (all(x in courses for x in EC1)) and float(gpa)>=7.00:
        e.append("EC1")
    if (all(x in courses for x in EC2)) and float(gpa)>=8.00:
        e.append("EC2")
    if (all(x in courses for x in EC3)) and float(gpa)>=0.00:
        e.append("EC3")
    if (all(x in courses for x in EC4)) and float(gpa)>=9.00:
        e.append("EC4")
    if (all(x in courses for x in EC5)) and float(gpa)>=6.00:
        e.append("EC5")
    if (all(x in courses for x in EC6)) and float(gpa)>=0.00:
        e.append("EC6")

    if not e:
        res.append(f'{name}[{", ".join(e)}]')
    else:
        res.append(f"{name}[{', '.join(e)}]")
print(res)

input and output required:

Rishi/CC1/CC2/7.59 Divya/CC3/CC5/CC2/5.2
["Rishi['EC1']", 'Divya[]']

Ria/CC1/CC2/CC3/9.59 Divya/CC3/CC5/CC4/8.2 Ayush/CC3/CC4/CC5/6.5
["Ria['EC1', 'EC2', 'EC3', 'EC4']", "Divya['EC6']", "Ayush['EC6']"]

basically i need double quotes around elements in res which have non empty e lists, and if the e list for some elemnt is empty its single quotes

0

There are 0 best solutions below