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 trial

Python

printing without the blank spaces at the end of each line?

Hi I created a quick project to apply what ive learned but I'd like to not have the blank spaces at the end of each line when printing. When I go to copy whats been printed in console, theres a long blank space at the end of each line. end= '' isnt working. maybe im not putting it in the proper spot for my script? please help.

def Change(old_subnet, new_subnet, proxies): for i in range(len(new_subnet)): proxies = proxies.replace(old_subnet[i], new_subnet[i]) return proxies print(Change(old_subnet, new_subnet, proxies))

3 Answers

Hi Cheyenne,

To omit the trailing newline you can use the 'end' argument to the print function.

'end' specifies what to put at the end of every print statement. By default it is a newline character. If you replace it with an empty string, you'll get the behaviour you are looking for:

print('one', end="")
print('long', end="")
print('word', end="")

Cheers

Alex

I actually have a 250 line string with triple quotes. With a function ive created that is replacing a section. Where in my function would I put end="" because I run into errors when i write it

def Change(old_subnet, new_subnet, proxies): for i in range(len(new_subnet)): proxies = proxies.replace(old_subnet[i], new_subnet[i]) return proxies print(Change(old_subnet, new_subnet, proxies))

HI Cheyenne,

Ok, so if the spaces are inside a single string then changing the terminator isn't going to help. Are you able to provide a small excerpt of the string that exhibits the behaviour you want to avoid so we can see what you are seeing? If you are able to post a sample of the string, make sure you follow the Markdown Cheatsheet linked below (especially the part about code snippets:

Code Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set >of backticks, that'll help us with syntax highlighting.

```html
<p>This is code!</p>
```

It's always helpful to have properly formatted code in these questions, but especially important when whitespace is significant as in your case.

This is the simple script i made following the replace method that will replace the old sub net section of my proxies with the new subnet and print it out. Everything is working fine but when it prints I need to be able to copy the print out without the blank spaces being copied in my clipboard when i copy.

old_subnet = ['126.126.126']
new_subnet = ['949.949.949']

proxies = """126.126.126.1:3128:user:pass 
126.126.126.2:3128:user:pass
126.126.126.3:3128:user:pass
126.126.126.4:3128:user:pass
126.126.126.5:3128:user:pass
126.126.126.6:3128:user:pass
126.126.126.7:3128:user:pass
126.126.126.8:3128:user:pass
126.126.126.9:3128:user:pass"""

def Change(old_subnet, new_subnet, proxies):
    for i in range(len(new_subnet)):
        proxies = proxies.replace(old_subnet[i], new_subnet[i])
    return proxies

print(Change(old_subnet, new_subnet, proxies))

Hi Cheyenne,

When I run your script I only get one space character copied to my clipboard on the end of the first line because that line, and only that line, in your input file has a single space character at the end of the line. So this is what I end up with:

949.949.949.1:3128:user:pass 
949.949.949.2:3128:user:pass
949.949.949.3:3128:user:pass
949.949.949.4:3128:user:pass
949.949.949.5:3128:user:pass
949.949.949.6:3128:user:pass
949.949.949.7:3128:user:pass
949.949.949.8:3128:user:pass
949.949.949.9:3128:user:pass

Note the space character at the end of the first line.

If you want a general way of removing any space characters that might be in your proxies variable, you can just use the replace method again on proxies with an old value of " " and a new value of "".

Adding that will get you the following output:

949.949.949.1:3128:user:pass
949.949.949.2:3128:user:pass
949.949.949.3:3128:user:pass
949.949.949.4:3128:user:pass
949.949.949.5:3128:user:pass
949.949.949.6:3128:user:pass
949.949.949.7:3128:user:pass
949.949.949.8:3128:user:pass
949.949.949.9:3128:user:pass

If you are concerned that you might be getting a broader range of whitespace characters as inputs into your proxies variable, then you might want to look at a regex to replace all amounts and types of whitespace with nothing, but if it's just space characters then using replace() with " " and "" is going to be your easiest solution.

Hope that helps,

Alex