4

2ND DATATYPE: STRINGS

by

The Education Machine

[support@theeducationmachine.com]

Strings Datatype

Strings can be called as a set of characters.

Values should come in the quotation marks (either ' or ") while doing variable declaration.

Make sure to balance the quotes in each statement to avoid any errors.


Synatx to define a string:

  name = 'Zebra'
  name2 = 'Donkey'
  print(name, type(name), name2, type(name2))


Output:
Zebra <class 'str'> Donkey <class 'str'>

ID of string datatype.

  name = 'Happy'
  address = 'Delhi'
  print(name, type(name), id(name))
  print(address, type(address), id(address))


Output:
Happy <class 'str'> 2320622797488
Delhi <class 'str'> 2320622797680

To print multiple information.

  age = 10
  empl_id = 1520
  name = 'Happy'
  address = 'Delhi'
  print("Please see my details below: ")
  print("Age:",age)
  print("Employee ID:",empl_id)
  print("Name:",name)
  print("Address:",address)


Output:
Please see my details below:
Age: 10
Employee ID: 1520
Name: Happy
Address: Delhi

To print the above result using one print statement.

  age = 10
  empl_id = 1520
  name = 'Happy'
  address = 'Delhi'
  print("Please see my details below: ", "
  Age:",age, "Employee ID:",empl_id, "Name:",name, "Address:",address)


Output:
Please see my details below:
Age: 10 Employee ID: 1520 Name: Happy Address: Delhi

To print the above output in multiple lines using one print statement.
This is possible with the help of escape character "\n" which adds a line or moves the pointer to new line.

  age = 10
  empl_id = 1520
  name = 'Happy'
  address = 'Delhi'
  print("Please see my details below: ", "\nAge:",age, "\nEmployee ID:",empl_id, "\nName:",name, "\nAddress:",address)


Output:
Please see my details below:
Age: 10
Employee ID: 1520
Name: Happy
Address: Delhi

To add space in the beginning of each line.
It is easy enough by adding few blank characters before beginning of each line.

  age = 10
  empl_id = 1520
  name = 'Happy'
  address = 'Delhi'
  print("Please see my details below: ", "\n   Age:",age, "\n   Employee ID:",empl_id, "\n   Name:",name, "\n   Address:",address)


Output:
Please see my details below:
   Age: 10
   Employee ID: 1520
   Name: Happy
   Address: Delhi

There is a better solution to it.
Another escape character '\t' can be usedto give a tab space at the beginning of each line.

  age = 10
  empl_id = 1520
  name = 'Happy'
  address = 'Delhi'
  print("Please see my details below: ", "\n\tAge:",age, "\\tnEmployee ID:",empl_id, "\n\tName:",name, "\n\tAddress:",address)


Output:
Please see my details below:
      Age: 10
      Employee ID: 1520
      Name: Happy
      Address: Delhi

What is the use of single quotes and double quotes?

In some cases, quotes are to be used wisely.
In below code,the python is assuming that code is ending at "n" because the code started with single quote.


  print('Pappu can't dance Sa***')


reserved_keywords

  print("Pappu can't dance Sa***")


Output:
Pappu can't dance Sa***

Alternative way to print above statement using single quotes only:'\' also works as an escape character which tells python that next character is raw text format.

  print('Pappu can\'t dance Sa***')


Output:
Pappu can't dance Sa***

Here python has assumed that "\n" is an escape character but user wanted to print it as a raw character.

  print("My python files are saved under: C:\kashish\documents\new")


Output:
My python files are saved under: C:\kashish\documents
ew

'\n' is being considered as new line character but we have to tell python that it is raw text, so let's use additional \ before \n.

  print("My python files are saved under: C:\kashish\documents\\new")


Output:
My python files are saved under: C:\kashish\documents\new

Another way for conversion of raw text is: using 'r' in the statement

  print(r"My python files are saved under: C:\kashish\documents\new")


Output:
My python files are saved under: C:\kashish\documents\new

How to insert comments in python?

Use "#" to comment a single statement
Use """ at start position and """ at end position if you want to comment multiple statements in one go