Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJamshaid Ali
189 PointsWhat 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
Peter Vann
36,427 PointsCommas 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!