Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:01:05+00:00 2026-06-17T11:01:05+00:00

Say I have this variables below ( id , a,b,c,d ) id a b

  • 0

Say I have this variables below ( id , a,b,c,d )

id  a b c d
x   2 4 5 7
y   4 5   9
z     1   2

I want to create a new concatenate variable named ‘total’ from these strings , so I used this code below :

total = a + ' ' + b + ' ' + c + ' ' + d

Since I don’t want all these to be next to each other 2457 , I need one space blank ( ' ' ) between each variables 2 4 5 7, my result look something like this

id  a b c d        total
x   2 4 5 7       2 4 5 7
y   4 5   9       4 5   9
z     1   2         1   2

My problem is .. for example @ y between 5 & 9 , I only want one space instead two Or i want my result to look like this … can anyone show me how to accomplish this ? In SAS , I can easily use something to compress , not sure how can I do this in python ..

id  a b c d        total
x   2 4 5 7       2 4 5 7
y   4 5   9       4 5 9
z     1   2       1 2

Hopefully I’m not confusing anyone ~ , thanks πŸ™‚

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:01:06+00:00Added an answer on June 17, 2026 at 11:01 am

    One of the reasons to use join instead of manually concatenating things is that you can do more complex stuff more easily.

    First, if you turn your a + ' ' + b + ' ' + c + ' ' + d into a join:

    ' '.join((a, b, c, d))
    

    That doesn’t change anything yet.

    2 4 5 7
    4 5   9
      1   2
    

    But now, how do we say “all of the non-empty strings in (a, b, c, d)“? Easy:

    ' '.join(x for x in (a, b, c, d) if x)
    

    So:

    2 4 5 7
    4 5 9
    1 2
    

    That’s it.

    If the empty values aren’t empty strings (or None) but, say, ' ', you need to change the test. For example, maybe:

    ' '.join(x for x in (a, b, c, d) if x.strip())
    

    If you don’t understand generator expressions, all of the following are roughly equivalent, and hopefully you’ll understand one:

    total = ' '.join(x for x in (a, b, c, d) if x)
    
    total = ' '.join([x for x in (a, b, c, d) if x])
    
    total = ' '.join(filter(bool, (a, b, c, d))
    
    non_zero_values = []
    for x in (a, b, c, d):
        if x:
            non_zero_values.append(x)
    total = ' '.join(non_zero_values)
    

    In every case, the idea is the same: We have a sequence of 4 values, and we’re filtering it down to a sequence of 0 to 4 values by keeping only the ones that aren’t empty.

    If we stuck with your explicit concatenation, this is still possible, it’s just much harder and uglier:

    ((a + ' ') if a else '' +
     (b + ' ') if b else '' +
     (c + ' ') if c else '' +
      d if d else '')
    

    Which again gives you:

    2 4 5 7
    4 5 9
    1 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have this program below. Now I want to pass both the
Possible Duplicate: How Do I Access This Variable? Lets say I have the code:
Let's say in PHP I have a string-variable : This takes between 5 and
I have a String variable say strHTML = <div class='abc'> This is a test
Let's say I have a CString variable carrying the string Bob Evans. I want
This might be a dumb question, but let's say I have a variable seconds
I'm confused about MVC routes. Let's say I have this domain: poopoopants.com. I want
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Say i have this this structure. MyApp β”œβ”€β”€ main.py └── package β”œβ”€β”€ __init__.py β”œβ”€β”€

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.