How do you concatenate a space in a string in python?

I have two strings:

>>> a = "abcd"
>>> b = "xyz"
>>> c = a + b
>>> c
abcdxyz

How can I get abcd xyz as a result instead when adding a and b?

How do you concatenate a space in a string in python?

msw

41.8k9 gold badges83 silver badges108 bronze badges

asked Jul 13, 2012 at 12:58

Simply just add a space between the two strings:

a = "abcd" 
b = "xyz" 
c = a + " " + b  # note the extra space concatenated with the other two
print c

this will give you

abcd xyz

You can use a function such as .join(), but for something so short, that would seem almost counter-intuitive (and IMO "overkill"). I.e., first create a list with the two strings, then call a function with that list and use the return of that function in a print statement ... when you can just concatenate the needed space with the 2 strings. Seems semantically more clear too.

Based on: "Simple is better than complex." (The Zen of Python "import this")

answered Jul 13, 2012 at 12:59

LevonLevon

132k33 gold badges197 silver badges186 bronze badges

3

You can use join to concatenate your strings together with your selected delimiter.

a = "abcd"
b = "xyz"
c = " ".join([a, b])

answered Jul 13, 2012 at 13:00

How do you concatenate a space in a string in python?

Christian WittsChristian Witts

11k1 gold badge31 silver badges44 bronze badges

Since Python 3.6, you can use f-strings to join two strings with ease. The code is more succinct and the variables are referenced directly within the context of the string.

a = 'hello'
b = 'world'
c = f'{a} {b}'

print(c)

The above code would output the following:

hello world

The .join() function is still the way to go when you have many strings or where the number of strings may vary.

answered Mar 1, 2019 at 16:33

How do you concatenate a space in a string in python?

noddynoddy

2,9373 gold badges24 silver badges20 bronze badges

Python supports the string formatting operations and a template system (the latter is technically a simple but powerful class) as part of the string module. While the plus operator does its work, the lack of string formatting can influence a lot the readability of the code. A basic example for string formatting:

c = '%s %s' % (a, b)

How do you concatenate a space in a string in python?

georg

206k48 gold badges292 silver badges373 bronze badges

answered Jul 13, 2012 at 13:28

How do you concatenate a space in a string in python?

VidulVidul

9,7162 gold badges17 silver badges20 bronze badges

2

Not the answer you're looking for? Browse other questions tagged python or ask your own question.

When I run this code it behaves like expected:

x = int(input("Put number: "))
result_figure =[]
xtempleft = x-1
xtempright = 0
space = " "
sl = "/"
bsl  = "\\"
#Q1
for i in range(x):
    if xtempleft > 0:
        q1= space * xtempleft + sl
        xtempleft -= 1
        print(q1)

    else:
        q1 = sl
        print(q1)
#Q2
for i in range(x):
    if xtempright == 0:
        xtempright += 1
        q2= bsl 
        print(q2)
    else:
        q2 = space * xtempright + bsl
        xtempright += 1
        print(q2)

I get this:

    /
   /
  /
 /
/
\
 \
  \
   \
    \

The problem is that when I try to do some modification:

for i in range(x):
    result =""
    if xtempleft > 0:
        q1= space * xtempleft + sl
        xtempleft -= 1
        result += q1     
    else:
        q1 = sl
        result += q1
#Q2
    if xtempright == 0:
        xtempright += 1
        q2= bsl 
        result += q2
    else:
        q2 = space * xtempright + bsl
        xtempright += 1
        result += q2  
    print(result)

to print what I need in the same line i get it like spaces from Q2 disappeared somewhere and didn't concatenate.

    /\
   / \
  /  \
 /   \
/    \

Anyone could help me with this? I have tried in many ways and can't get it.

How do you concatenate a space in a string in python?

jww

92.6k85 gold badges380 silver badges833 bronze badges

asked Jul 31, 2017 at 11:37

1

In modification you omitted the for loop.

In your initial code there are two for loops and in your modification you omitted the second for loop.

answered Jul 31, 2017 at 11:46

How do you concatenate a space in a string in python?

RahulRahul

10.2k4 gold badges49 silver badges86 bronze badges

1

As the / goes one position left on each line, you need one additional space before the \. It is enough to write in Q2:

    q2 = space * 2 * xtempright + bsl

answered Jul 31, 2017 at 11:56

How do you concatenate a space in a string in python?

Serge BallestaSerge Ballesta

138k11 gold badges114 silver badges234 bronze badges

1

If this is your expected output,

    /\
   /  \
  /    \
 /      \
/        \

Modify #Q2 to :

if xtempright == 0:
        xtempright += 1
        q2= bsl 
        result += q2
    else:
        q2 = space * (xtempright+1) + bsl
        xtempright += 2
        result += q2

answered Jul 31, 2017 at 11:52

SajinSajin

1,5981 gold badge13 silver badges18 bronze badges

0

Not the answer you're looking for? Browse other questions tagged python concatenation spaces or ask your own question.

How do I concatenate a string with a space?

There are two ways to do this:.
Add double quotation marks with a space between them " ". For example: =CONCATENATE("Hello", " ", "World!")..
Add a space after the Text argument. For example: =CONCATENATE("Hello ", "World!"). The string "Hello " has an extra space added..

How do you concatenate special characters in Python?

String Concatenation is the technique of combining two strings. String Concatenation can be done using many ways..
Using + operator..
Using join() method..
Using % operator..
Using format() function..
Using , (comma).

How do you concatenate a string with a comma in Python?

Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string. If a newline character \n is used, a newline will be inserted for each string.

How do you concatenate two strings without space in Python?

Method 2: Remove spaces from a string using split() and join() First, we use the split() function to return a list of the words in the string, using sep as the delimiter Python string. Then, we use join() to concatenate the iterable.