Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to remove last comma from print(string, end=“, ”)

my output from a forloop is

string = ""
for x in something:
   #some operation
   string =  x += string 

print(string)

5
66
777

I use the below code to have them on the same line

print(string, end=", ")

and then I get

5, 66, 777,

I want the final result to be

 5, 66, 777

How do I change the code print(string, end=", ") so that there is no , at the end

The above string is user input generated it can me just 1 or 2,3, 45, 98798, 45 etc

So far I have tried

print(string[:-1], end=", ")                   #result = , 6, 77, 
print((string, end=", ")[:-1])                 #SyntaxError: invalid syntax  
print((string, end=", ").replace(", $", ""))   #SyntaxError: invalid syntax  
print(", ".join([str(x) for x in string]))     # way off result 5
                                                                6, 6
                                                                    7, 7, 7
    print(string, end=", "[:-1])                  #result 5,66,777,(I thought this will work but no change to result)
   print(*string, sep=', ')                          #result 5
                                                             6, 6
                                                             7, 7, 7 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

user input
twenty five, four, nine 

gets

25, 4, 9, #(that stupid comma in the end)
like image 413
Samir Tendulkar Avatar asked Sep 07 '18 22:09

Samir Tendulkar


People also ask

How do you not print a comma at the end of a string?

To remove the last comma from a string, call the replace() method with the following regular expression /,*$/ as the first parameter and an empty string as the second. The replace method will return a new string with the last comma removed. Copied!


2 Answers

You could build a list of strings in your for loop and print afterword using join:

strings = []

for ...:
   # some work to generate string
   strings.append(sting)

print(', '.join(strings))

alternatively, if your something has a well-defined length (i.e you can len(something)), you can select the string terminator differently in the end case:

for i, x in enumerate(something):
   #some operation to generate string

   if i < len(something) - 1:
      print(string, end=', ')
   else:
      print(string)

UPDATE based on real example code:

Taking this piece of your code:

value = input("")
string = ""
for unit_value in value.split(", "):
    if unit_value.split(' ', 1)[0] == "negative":
        neg_value = unit_value.split(' ', 1)[1]
        string = "-" + str(challenge1(neg_value.lower()))
    else:
        string = str(challenge1(unit_value.lower()))

    print(string, end=", ")

and following the first suggestion above, I get:

value = input("")
string = ""
strings = []
for unit_value in value.split(", "):
    if unit_value.split(' ', 1)[0] == "negative":
        neg_value = unit_value.split(' ', 1)[1]
        string = "-" + str(challenge1(neg_value.lower()))
    else:
        string = str(challenge1(unit_value.lower()))

    strings.append(string)

print(', '.join(strings))
like image 127
Joshua R. Avatar answered Oct 25 '22 00:10

Joshua R.


If you can first construct a list of strings, you can then use sequence unpacking within print and use sep instead of end:

strings = ['5', '66', '777']

print(*strings, sep=', ')

5, 66, 777
like image 27
jpp Avatar answered Oct 25 '22 01:10

jpp