Home > Enterprise >  Remove white space between strings in Python
Remove white space between strings in Python

Time:09-14

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.

CodePudding user response:

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.

CodePudding user response:

I'm not sure if this what you was look for but it may be you answer.

import string x=string.ascii_lowercase

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

CodePudding user response:

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

  • Related