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

  • Home
  • SEARCH
  • 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 9002425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:34:09+00:00 2026-06-16T00:34:09+00:00

I was trying to use a ternary to print out a string if something

  • 0

I was trying to use a ternary to print out a string if something was true and something else if it wasn’t (simple enough, right?). It gives no errors and doesn’t seem like it would cause any trouble. However, when running…

print('[',dr+1,']\t',d[dr] if fullpath else '[',dr+1,']\t',d[dr].split("/")[len(d[dr].split("/"))-2], sep='')

…it prints out

'[',dr+1,']\t'

twice.
For example, it prints this:

[1] [1] Accoustic
[2] [2] Classical
[3] [3] Epic
[4] [4] Rock
[5] [5] Spoof
[6] [6] Techno & Electronic

If you want, in order to see what the variables are (which shouldn’t matter since this is just a concept issue), you can see the rest of the code here

Thinking that it might be the variables, I tried printing the statements separately (no ternary) and they came out as I expected (i.e. without the odd duplication). Knowing that the variables were fine, I tried enclosing the strings with parenthesis (in an attempt to keep the strings from mixing) but, since python interprets them literally, they came out as arrays instead of strings. Also, + symbols (in lieu of commas) don’t work because python can’t combine the data type. I ended up changing the statement to

print('[',dr+1,']\t',d[dr]) if fullpath else print('[',dr+1,']\t',d[dr].split("/")[len(d[dr].split("/"))-2], sep='')

and it works fine.

Not so great coding aside, why is that first string getting printed twice?

  • 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-16T00:34:10+00:00Added an answer on June 16, 2026 at 12:34 am

    Allow me to break it down for you…

    print(('['), (dr+1), (']\t'), (d[dr] if fullpath else '['), (dr+1), (']\t'), (d[dr].split("/")[len(d[dr].split("/"))-2]), sep='')
    

    See the problem?

    print(*(('[',dr+1,']\t', d[dr]) if fullpath else ('[',dr+1,']\t', d[dr].split("/")[len(d[dr].split("/"))-2])), sep='')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the conditional (ternary) operator to assign the proper lambda
I'm new to Python and I'm trying to use ternary opertor which has this
Have an Action delegate and trying to use the ternary operator inside of it
I'm trying to use the ternary to return differing types, although I seem to
I have a simple question that boggles me. I am trying to use the
I'm trying to use a ternary operator in Razor, similar to this question ,
I have come across something a little odd. I as trying to use the
I'm trying use an object which wasn't available until SDK level 5. It seems
I'm trying use preg_match in an IF statement and return false if a string
I have a case statement and I am trying to use the ternary operator

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.