PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 16, 2022

[FIXED] How to use end="" parameter in f-string print format?

 September 16, 2022     for-loop, loops, printing, python, string     No comments   

Issue

afs="Hello, World! KD herfe"
print("[",end="")
for ad in afs:
    if ad!=" ":
        # print(ad,end="")
        print(f"{ad}")
    else:
        print("",end="")
print("]")

Here I wanted to print afs using f-string in this form

[Hello,World!KDherfe]

Output is right if commented f-string version but I wanted to know how use end parameter in f-string.

enter image description here


Solution

afs="Hello, World! KD herfe"
print("[", end="")
for ad in afs:
    if ad != " ":
        print(f"{ad}", end="")

print("]")

You can just use end="" normally after a comma in any print statement. It is a keyword argument (often called a kwarg). It does not matter if your first argument is a string (ie: "["), a variable (ie: ad) or an f-string (ie: f"{ad}").

For what it's worth you can achieve the same thing by calling replace() on an f-string in a single print statement.

afs="Hello, World! KD herfe"
print(f"[{afs}]".replace(" ",""))


Answered By - Zhenhir
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing