Remove white space between strings in Python

Viewed 42

I am doing some practice code exercises, and the question is asking to "Provide a script printing every possible pairs of two letters, only lower case, one by line, ordered alphabetically." Basically you should print out

aa
ab
a
..
ba
bb
..
zz

I've got the two loops going fine, but I can't remove the white space between the returned valued. So I am printing out

a a 

rather than

aa

Here is my code

import string
x=string.ascii_lowercase

for i in x:
    for j in x:
        print(i, j).strip()

This question has been answered elsewhere, but I don't understand any of the answers. Thanks.

3 Answers

You can just concatenate the strings together as such

import string
x=string.ascii_lowercase

for i in x:
    for j in x:
        print(i+j)

or

import string
x=string.ascii_lowercase

for i in x:
    for j in x:
        print(i,j ,sep="")

which removed the space between the two variables and you can change it to whatever you want

I would go with this:

import string
x=string.ascii_lowercase

for i in x:
    for j in x:
        print(i + j)

The output seems to match your idea.

I'm not sure if this is what you were looking for, but it may help:

import string
x=string.ascii_lowercase

for i in x:
    for j in x:
        print(i+j)
Related