简体   繁体   中英

How to remove commas from printed result?

I need to print this list without the commas, but I can't seem to figure out how.

data = (
    ('Sabrina Bryan,1/3/98,ss9387,f9;s1;m1,99km'),
    ('Fergus Connon Archer,11/21/89,ss5246,f1;s3,102km'),
    ('Adrian Harvey,3/3/78,ss1654,m5;s2,72km'),
    ('Patricia Abigail Wolf,9/5/00,ss0936,f3;s4;m8,134km'),
    ('Georgina Kramer,6/15/95,ss4837,f5;s2;m1,55km'),
    ('Glenn Julian Ayala,3/19/90,ss3689,s4;f3,152km'),
    ('Anita Davila,6/27/91,ss9367,f8,203km'),
    ('Gertrude Nunez,1/12/97,ss3948,s3;m1,34km'),
    ('Solomon Burton,8/5/88,ss7364,s2;f1,23km'),
    ('Rafael John Murray,10/19/01,ss9105,s9;f3,78km')
)

info = input("What would you like to do? ")
if info == 'b':
    for line in data:
        fname0 = data[0].split(' ')
        lname0 = fname0[1].split(',')
        bday0 = lname0[1].split('/')

        def Sabrina(fname0, bday0):
            print(fname0[0], *bday0[0:2], sep = ', ')
        print(Sabrina(fname0, bday0))

It is printing "Sabrina, 1, 3" when I want it to print "Sabrina 1 3". I have tried using the .replace command, *bday0[0:2].replace(',', ' ') , but it just gives me an error. Any tips?

You don't need .replace() .


print(fname0[0], *bday0[0:2])

or

print(fname0[0], *bday0[0:2], sep = ' ')

will give you the desired output.

在此处输入图像描述

I figured out a quick solution for your problem:

It is based on the use of F-Strings . With this formatting-solution, you can quickly format your strings by implementing your variables without losing sight of the format of your output, also adding a return to the function.

Function Code Snippet

 def Sabrina(fname0, bday0):
        return f"{fname0[0]}, {bday0[0]} {bday0[1]}"

Here is more information related to the f-strings formatting explained .

It would be a great improvement if you could work around the issue that the values are hardcoded. But for a quick implementation this should help.

Let me know if this answers your question. Cheers!

Full Working Code-Snippet

data = (
('Sabrina Bryan,1/3/98,ss9387,f9;s1;m1,99km'),
('Fergus Connon Archer,11/21/89,ss5246,f1;s3,102km'),
('Adrian Harvey,3/3/78,ss1654,m5;s2,72km'),
('Patricia Abigail Wolf,9/5/00,ss0936,f3;s4;m8,134km'),
('Georgina Kramer,6/15/95,ss4837,f5;s2;m1,55km'),
('Glenn Julian Ayala,3/19/90,ss3689,s4;f3,152km'),
('Anita Davila,6/27/91,ss9367,f8,203km'),
('Gertrude Nunez,1/12/97,ss3948,s3;m1,34km'),
('Solomon Burton,8/5/88,ss7364,s2;f1,23km'),
('Rafael John Murray,10/19/01,ss9105,s9;f3,78km'))

 info = input("What would you like to do? ")
 if info == 'b':
 for line in data:
      fname0 = data[0].split(' ')
      lname0 = fname0[1].split(',')
      bday0 = lname0[1].split('/')

    def Sabrina(fname0, bday0):
        return f"{fname0[0]} {bday0[0]} {bday0[1]}"
    
    
    print(Sabrina(fname0, bday0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM