More and more strings.

2 37
Avatar for Vernardo
2 years ago

Python3 Article #0.3

Background story.

If you follow my python series this is the continuation story of how I pursue programming in python

When my mother discovered that I put the phone into a PUK mode she did not do anything to me and I thought it was not that important as she can just change the sim card and use the phone as if nothing happened my curiosity inexorably creep on me as days went by.

More and more strings.

By this time if you practice printing anything to the interpreter you might have a general knowledge of how the built-in function work that's right print statement is a built-in function.

Now let's try to use some built-in function string formatting.

name = 'PanPanHam'
print('My name is ' + name + 'My name is ' + name + 'My name is ' + name)

As you can already guess it should print our name 3 times but if for example we want to print our names 100 times it will be hard to type 'My name is ' + name 100 times right?

That's where string formatting comes in handy.

print('My name is %s' % name * 100)
Sponsors of Vernardo
empty
empty
empty

Easy peasy the %s is a string formatting method in python we can place that to where we want to specify where it should be and the * operator is a times in python the 100 is an integer we tell that we want to print that statement with the help of string formatting and times that to one hundred.

Don't forget to put the opening ( parenthesis and the closing parenthesis) when you put an apostrophe in python value it converts that into a string literal you can't chain or concatenate strings and integers in python.

Example:

type('1')
type(1)

str is short for strings and int is short for integers.

print('1' + 1)
print('1' * 1)
print('1' * 2)

the first line of code will throw an error saying you can only concatenate string to string not integer.

But you can times how many you want to print a certain string.

Let's all try this one more time what we all learn.

var1 = 'Millionaire'
var2 = 'Help'
var3  = 'Happy'
print('I want to become a %s and %s others so they will be %s everyday' % (var1, var2, var3) * 3)

We just tell the computer to print our string formatting three times and with three different strings we need to open and close our string variable and put comma , to tell the computer this is the one I want to put here and there.


I hope you learn something here and if you want me to cover more of this tutorial you can help me do so by sharing your thought in the comment section below or subscribe to my read.cash so you can follow all of my articles regarding tutorials in python.

Leading Image: Python Logo


My other social media where you can contact me for anything.

NoiseCash: noise.cash/@Vernardo

Telegram: PanPanHam

4
$ 0.04
$ 0.03 from @GarrethGrey07
$ 0.01 from @SuperJulalaine
Avatar for Vernardo
2 years ago

Comments

Are you an IT? When I was studying computer terms I was a little "hang" since some words I hear are for the first time. What is python?

Was it your mother's phone? Haha.

$ 0.00
2 years ago

I'm not an IT or an expert in the field I just learn all of it by self-studying I hold no bachelor's degree or any bragging rights to my name.

Basically Python is a programming language, programming language is use to build website and application or games If I can compare read.cash website it is close to flask or django web frameworks that is created using Python programming language.

$ 0.00
2 years ago