How to style out put using python string format - AmmadKhalidsteemCreated with Sketch.

in #programming7 years ago

Hello everyone,
In this tutorial I will show you how we can style our output using string format in Python.

Some of you guys should saw in Python scripts that they are printing data like:

[+] Message | Output [+]

And this kind of printing is really looks cool to me.

Some of you guys should try something like this:

print("[+] your message [+]")

Or:

print("[+]", "your message", "[+]")

The second one is looks little bit clearer
Isn't it?, Cause of print function.

But let's suppose we are working on it large scale application and we want to print in stylish way.

But it's weird that we always have to use our style tag like:

[+] or [#]

So on every output we have to keep using our style tag but its consider as bad practice in coding to me.

As a good programmer we should try to code clean and it can be easily understand.

Whenever I code in Python my focus is write clean code and I divide the parts of program into modules (sub programs).

So for this, I always code in OOP (object oriented programming) of python.

In modules I create classes, their functions and I use them into main module by importing.

But anyway in OOP way, I will show you

How to style out put using python string format in Python

Let's create class in Python:

Class test:
    def __init__(self):
           self = self

Some of you or all notice that why I use constructor function

Well because I inherit other module classes like I create an helper module which have a helper class and that class have those functions, who will in every module.

anyway let's continue:

 Class test:
    def __init__(self):
           self = self


    def print(self, *msgs, style = '[+]'):
          msg = " ".join( str(msg) for msg in msgs )
          print(style, msg, style)

And now in our class we can use something like:


Class test:
    def __init__(self):
           self = self

    def print(self, *msgs, style = '[+]'):
          msg = " ".join( str(msg) for msg in msgs )
          print(style, msg, style)

    def  test(self):
            self.print("test")

you will output will be like this:

[+] test  [+]

cool right?
I know this is not good way to style your output. But it's better if we don't touch builtin functions isn't it?
But don't worry in my next tutorial i will How to style out put using python string format without in OOP way.

Thanks for reading and i write this article cause i can't sleep and it's 5 AM here. My brain is still stuck in coding.

i will be keep posting but mostly in weekend.
Why not check my old one articles?
1. Are hacking minigames the most overused of gaming tropes?
2. HOW TO GET ADSENSE APPROVED 100% WORKING METHOD
3. HOW TO UNBLOCK ANY SITE WITHOUT USING VPN OR PROXY! - EASY LIFE WITH EASY WAYS

BLOG POST LINK

Sort:  

My wife is a programmer and she is just as crazy, she does not sleep at night and in her mind she is always processing information hahaha, even though she talks to me about her work I will never understand what she says. Greetings from Venezuela .

Congratulation

Today one year ago you joined SteemIt
Thank you, for making SteemIt great and Steem on for more years to come!

(You are being celebrated here)

Thanks for the post.

Nice one young programmer..

Sangat membantu sekali, terimakasih atas tutorialnya, sukses terus. Salam

nice work, super photography upovted, hope you also will check my post,, please

Greetings from Venezuela!

sangat membantu, bantu sesama ya mas ,..

Congratulations! 🎉 Your Steemversay has arrived. One year ago today you made your steem account along with 177 others. You are one of 9 users who have posted in the last week. Well done you.

I've upvoted your post, I hope it helps. Happy Steemversay ✌️;

p.s I'm a new bot, I've only been alive 20 days :)
Learn more about SteemVersary and view awards at Steemversary

thank for sharing your tutorial, very good post...
and thank have your following my steemit, I also followed you @ammadkhalid

Coin Marketplace

STEEM 0.19
TRX 0.14
JST 0.029
BTC 66930.79
ETH 3268.09
USDT 1.00
SBD 2.64