Im just starting out and for a assignment i need to print this line: Print (variable_a, “filler text”) But there can’t be a space between the two. How do i remove this space?
python - How to print variables without spaces between values - Stack Overflow
How do I remove space at the end of an output in python? - Stack Overflow
How to Remove Spaces From a String in Python (Complete 2026 Guide)
pycharm - How to remove space from print statement in Python? - Stack Overflow
Don't use print ..., (with a trailing comma) if you don't want spaces. Use string concatenation or formatting.
Concatenation:
print 'Value is "' + str(value) + '"'
Formatting:
print 'Value is "{}"'.format(value)
The latter is far more flexible, see the str.format() method documentation and the Formatting String Syntax section.
You'll also come across the older % formatting style:
print 'Value is "%d"' % value
print 'Value is "%d", but math.pi is %.2f' % (value, math.pi)
but this isn't as flexible as the newer str.format() method.
In Python 3.6 and newer, you'd use a formatted string (f-string):
print(f"Value is {value}")
Just an easy answer for the future which I found easy to use as a starter:
Similar to using end='' to avoid a new line, you can use sep='' to avoid the white spaces...for this question here, it would look like this:
print('Value is "', value, '"', sep = '')
May it help someone in the future.
Why don't you use list comprehension and str.join?
print(' '.join([w for w in ip if item in w]))
I don't think there's a way to remove that, as it's a part of your terminal. Best answer I can give you.
I expanded on the code though, cause I was kinda bored.
sentence = input("Enter a sentence: ").lower()
pull = input("Which character(s) do you want to count?: ").lower()
for c in pull:
occurrences = 0
for character in sentence:
if c == character:
occurrences+=1
if c!=" ": print("\'%s\' appears %d times"%(c, occurrences))
for word in sentence.split():
occurrences = 0
for character in word:
if c == character:
occurrences+=1
if occurrences == 1:
print(("1 time in \'%s\'")%(word))
elif occurrences > 0:
print(("%d times in \'%s\'")%(occurrences,word))
You should get accustomed to using python's f-strings. You could use an f-string to print this statement like this:
print(f"The sum of 1-9 is {1+2+3+4+5+6+7+8+9}.")
The f-string setup will just replace whatever is between the {} with the expression - in this case, it'll do the sum and result in a single number. You can also put in a variable name or any other expression.
You could use string formatting:
print("The sum of 1-9 is %s." % str(1+2+3+4+5+6+7+8+9))
Hello,
I am using a very basic local function. In the output, space is showing after variable, and I don't know how to remove the space.
def jhn():
a = "John"
print("Hello,",a,"." "This is a local Variable")
jhn()
This output is showing "Hello, John .This is a local Variable"
As you can see there is space between .(dot) and John. Can someone let me know how can I remove the space. The output should be "Hello, John. This is a local Variable"
If you want to do it from print() , you can use sep argument. Example -
print("Total: $",total,sep='')
By default (if no sep parameter is specified) Python uses ' ' (space) as sep, to separate each different argument to print() function , and that is why you get a space inbetween your $ and total . Using above method we change that to an empty strig.
Demo -
>>> total = 123
>>> print("Total: $",total,sep='')
Total: $123
Or you can use str.format that would give you more control on formatting your output. Example -
print("Total: ${}".format(total))
print("Total: $" + str(total))
Use:
print ('\"',the_tuple[1],'\"', sep='')
^^^^^^
Note that those escapes are completely unnecessary:
print ('"', the_tuple[1], '"', sep='')
Or even better, use string formatting:
print ('"{}"'.format(the_tuple[1]))
Another f-string example,
the_tuple = (1,2,3,4,5)
print(f'"{the_tuple[1]}"')
gives
"2"
Better use string formatting:
print('\n{} you will be {} in ten years.'.format(name, ageinten))
or use sep='', but then you'd have to add trailing and leading spaces to the strings.:
print("\n", name, " you will be ", ageinten, " in ten years.", sep='')
Default value of sep is a space, that's why you're getting a space.
Demo:
>>> name = 'Example'
>>> ageinten = '20'
>>> print("\n",name," you will be ",ageinten," in ten years.", sep='')
Example you will be 20 in ten years.
>>> print('\n{} you will be {} in ten years.'.format(name, ageinten))
Example you will be 20 in ten years.
Try using just print() to give out newlines. This seems to fit your current style most:
print("Let's find out how old you will be in 10 Years.\n")
name = input("name: ")
print()
print("Now enter your age,",name)
print()
age = int(input("age: "))
ageinten = age + 10
print()
print(name,"you will be",ageinten,"in ten years.")
input("Press Enter to close")