Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Meet Python Variables

What are the comma's used for i didnt quite understand there usage? Video : (Variables Python )

WHat are the commas usd for in python when writing a variable or using strings.

1 Answer

Commas used inside the print statement allow you to concatenate smaller strings (text tidbits, essentially) into one long string. In that new string, wherever the comma appears, it is replaced automatically by a space (" "). This concatenation ability allows you to also insert (the values of) variables into that long string.

Consider this code snippet:

full_name = "Jamshaid Ali"
language = "Python"

print(full_name, "is well on the way to being a", language, "guru!")

will print:

Jamshaid Ali is well on the way to being a Python guru!

You can also achieve the same result this way:

full_name = "Jamshaid Ali"
language = "Python"

print(full_name + " is well on the way to being a " + language + " guru!")

Notice that if you concatenate with the + you need to add the spaces to the text. Using the comma, you don't.

You can play with it here:

https://www.katacoda.com/courses/python/playground

More info:

https://www.programiz.com/python-programming/methods/built-in/print

Bonus (other ways to do similar things):

https://www.programiz.com/python-programming/string-interpolation

I hope that helps. Happy coding!