Ticker

6/recent/ticker-posts

Advertisement

Everything you need to know about Strings and Variables in Python

String, variable in python, python tutorial
          Pic credit: Flaticon


Strings:
             While you have already been writing strings, you still don't know what they do. Every time you put " (double - quotes) around a piece of text you have beem making a string. A string is how you make something that your program might give to  a human.  In python strings are surrounded by either single quotation marks ' or double quotation marks "  and sometimes triple  quotation  marks  """. You saw this many times with your use of print,  when you put the text you want to go to the string inside "  or  ' after the print. Then Python prints it.Strings are Immutable. This means that you cannot changed, onece they defined. Many python methods such as join(), replace() or split modifiy the strings but they do not able to modify the original strings, they simply create a copy of a  string which they  modify and returned to the caller.  Lets take a look at the following examples of strings:
a =  ' Hello, python world! '

b = " Hello, python world! "

c = "" Hello, python world! """

print(a)
print(b)
print(c)

If you run this example in python console, the outputs  of a, b and c will be the same (Hello, python world!).


Variables : 
                      
                  In programming a variable  is nothing more than a name for something so you can use the name rather than something as you code. Programmer use these variable name to make their code read more like English and because they have lousy memories:). If they didn't use good names for things in their software, they'd might  lost when they tried to read their code again. In other words A variable is like a box in the computer's memory where you can store a single value. If you want to use the result of an evaluated expression later in your program, you can save it inside a variable.

You'll store values in variables with an assignment statement. An assignment statement consists of  variable name, an equal sign( = called the assignment operator) and the value to be stored. If you enter the assignment statement  a = "Hello, Python world!", then a variable named spam will have string value "Hello, Python world!" Stored in it. 

a = "Hello, Python world!"

Print(a)

You can print a  named variable instead of "Hello, Python world!" String.


So, that's all for today, If you find this article is helpful for  you then don't forget to share with people who are in need. I'll be back with another article soon. Stay tuned stay happy:) and have a great day. 
   











                

Post a Comment

0 Comments