Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

# How to Put a Comma After a Variable in Python

Put a Comma After a Variable in Python? In this article, we will learn how to add a comma after a variable in Python.

We have different methods to put a comma after a variable:-

• Using the join() function.
• Using a for loop.
• Using the replace() function.

# How to Put a Comma After a Variable in Python Using the join() function

The join() function is capable of concatenating a new string with a comma added after every character. This process can be done seamlessly in just a single line of code.

``````x = "example"
y = ','.join(x)
print(y)``````

Output:-

E,x,a,m,p,l,e

# Using the for loop

A for loop can be utilized to manually iterate through each character of the given string and add a comma after every single one of the characters.

``````x = "Example"
y = ' '
for i in x:
y += i + ','*1
y = y.strip()
print(repr(y))``````

Output:-

‘E,x,a,m,p,l,e,’

# Using the replace() Function

The replace() function carries out the basic task of replacing one specified phrase with another.

``````def commaez(x):
return x.replace('', ',')[1:-1]

x = "example"
print(commaez(x))``````

Output:-

E,x,a,m,p,l,e

So, here we have discussed different methods to add a comma after a variable or a string or character. Hope you all find the post useful.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!