Sunday, 3 October 2021

Python strings

A string is a sequence of characters. Which is an array of bytes representing Unicode characters. A string character is simply a string with a length of one. The square bracket [] can be used to access elements of the string.

·    Strings in python are surrounded by either a single quotation mark or a double quotation mark.

·    Like ‘Hello python’ or “Hello python”.

print('Hello Python')

print("Hello Python")



Single-line string

·         To assign a single line string to a variable, use this:

Example

a = "Hello Python"

print(a)



Multiline string

·         To assign the multiline string to a variable, use three single quotes or three double-quotes.

Example

a = '''Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque earum eius, sapiente error quos distincti est recusandae necessitatibus provident'''

print(a)



Example

a = """Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque earum eius, sapiente error quos distincti est recusandae necessitatibus provident"""

print(a)



Note:- The line breaks are inserted at the same position.

Looping a string

·         We can loop through the characters in a string with a “for” loop.

Example

for x in "Hello":

    print(x)



String length

·         The len() function returns the length of string.

Example

a = "Hello Python"

print(len(a))




Thursday, 9 September 2021

Program in python

As we learned about syntax in our previous article which is the introduction of python. Python syntax can be executed by writing directly in the command line.

      print("Hello, Python!")

To create a python file in the IDE, we use the .py file extension and run it in the command line.

    c:users\ name >python file.py

Python Indentation

In python, indentation is very important it’s used to indicate a block of code. Where in another programming language the indentation in the code is for readability only.

Example

if 2 > 1:

  print("one is smaller than two!")

Note:- python will give an error if skip the indentation.

Example

Syntax error:

if 2 > 1:

print("one is smaller than two!")

Comment in python

·         It is used to explain the python code.

·         It is used to make the code more readable.

·         It is used to prevent execution when testing code.

·         Create a comment in python with #, and python will ignore them.

 

Example

                #This is a comment

             print("Hello, Python!")

 

Example

      print("Hello, python!") #This is a comment

 

Example

   #print("Hello, Python!")

             print("hello, python3.9!")

Multiline comment

·        Python uses triple quotes for long comments in the python program.

·         To add multi-line comments insert a # for each line.

Example

   #This is a comment

   #written in

   #more then just one line

  print("Hello, Python!")

Example

            """This is a comment

                 written in

                 more than just one line"""

               print("Hello, Python!")