0

I am using parameterized functions in python. And I am passing some string value and access its value.

This is how my function definition looks like:

import constants

def print_error_table(self, header1, errormsg, icon):
        # for header1
        print(constants.GREEN)
        print(constants.REPORT_HEADER_ERR)
        print(constants.DATE_TIME)
        print(constants.SPACE)
        start = "|   |           "+constants.ICON_BOX

        header1_count = len(header1)

        available_space_for_first_part = 37 - (len(start) + header1_count)

        s = ""
        print(constants.REPORT_ROOF)
        print(constants.REPORT_COLUMNS)
        print(constants.REPORT_FLOOR)
        print(constants.REPORT_MIDDLE)
        print(start),
        print(" "+constants.NC+header1+constants.GREEN),

        for i in range(available_space_for_first_part):
             s += " "

        print(s),
        print("|"),

        # right part
        end = "                "
        end2 = "|    |"
        s2 = ""
        icon_count = len(icon)
        available_space_for_second_part = 31 - (len(end) + icon_count)
        print(end),
        print(icon),
        for i in range(available_space_for_second_part):
            s2 += " "
        print(s2),
        print(end2)

        print(constants.REPORT_SHORT_HORIZONTAL_LINE)
        print(constants.REPORT_MIDDLE_NO_MIDDLE_SEPTUM)


        # print(len(constants.SPACE)) # 84

        # print first 40 characters
        start = "|   |"
        print(start),
        s3 = ""

        for i in range(12):
            print(" "),
        print(constants.RED),
        msg = "1.) " + errormsg,
        print(""+msg), # this is where my error is getting

        print(constants.GREEN),

        # print(constants.RED+constants.ICON_CROSS+msg+constants.GREEN),

        for i in range(12, 57 - len(msg)-1):
            s3 += " "
        print(s3),
        print("|    |"),
        print("")
        print(constants.REPORT_MIDDLE_NO_MIDDLE_SEPTUM)
        print(constants.REPORT_MIDDLE)
        print(constants.REPORT_FLOOR)
        print(constants.REPORT_FOOTER)

And this is my other python file where I am calling this function from.

error_message = "\"etcd\" is impaired\n"
print_error_table(self, "ETCD", error_message, constants.ICON_CROSS)

And I get this error:

File "/home/jananath/Desktop/python-script/2/bitesizetrouble/report_error.py", line 57, in print_error_table print(""+msg), TypeError: cannot concatenate 'str' and 'tuple' objects

The problem is the value I am passing (i.e error_message) is not passing as a string it is some kind of an altered text for some reason.

I am telling this because, in above (first) command, where it says print(""+str(msg)), instead of this, when I try print(msg), it gives some weird output like this.

(`"etcd" is impaired\n',)

You can see two parenthesis are there on each side. Where does it come from and why I cannot concatanate the string I am passing to the function with another string (i.e print(""+str(msg))

update: I am using , to stop print() printing a new line. This is the way I should do that in python 2.7.5

8
  • I notice that a lot of your code's statements end with a comma. If you're under the impression that it behaves like a semicolon in languages such as C or javascript, that's not the case. A trailing comma typically turns the object preceding it into a tuple. Try deleting all of them, in particular the one at the end of msg = "1.) " + errormsg,, and see if that helps. Commented Dec 30, 2019 at 15:59
  • If you are only just learning Python, you should probably abandon Python 2 and concentrate on learning the currently recommended and supported version of the language, which is Python 3. Commented Dec 30, 2019 at 15:59
  • @Kevin I am using , to stop print() printing a new line. This is the way I should do that in python 2.7.5 Commented Dec 30, 2019 at 16:00
  • @tripleee, no I am using python 2.7.5 for reason. I can't change back to the newest version. Commented Dec 30, 2019 at 16:01
  • The trailing comma is specific to print but you have sprinkled it in a lot of places where it doesn't do that. It creates a tuple and now you are annoyed that you have a tuple. Commented Dec 30, 2019 at 16:03

1 Answer 1

3

Your code is full of commas and you put one more in the wrong place.

You create a tuple with one element by adding a comma to the end of string assignment. See:

>>> foo = "bar"
>>> print(foo)
bar
>>> foo = "bar",
>>> print(foo)
('bar',)

I would recommend you to start with Python tutorial at python.org (https://docs.python.org/3/tutorial/index.html). Seriously. And do not learn Python 2, it's no more. Seriously.

Sign up to request clarification or add additional context in comments.

4 Comments

As you can see, I have updated the question. I need some of these printing outputs to be printed in the same line without printing new lines. And I SHOULD use python 2.7 for some reason. I can't switch back to the older version. Is there another way i can prin in single line without really printing new lines?
@JananathBanuka My answer answers your question. Here your code creates a tuple of string because of trailing comma: msg = "1.) " + errormsg, -- and it has nothing to do with printing.
Yes this works, but the string I am passing is still buggy. It gives many characters (like hidden ones) when I get the output. When I just give a random output, it prints without any abnormalities.
@JananathBanuka stackoverflow.com/help/minimal-reproducible-example is useful reading and may help you to get better answers faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.