Page 427 - CITS - Computer Software Application -TT
P. 427
COMPUTER SOFTWARE APPLICATION - CITS
Python String
Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes.
In Python, strings can be created by enclosing the character or the sequence of characters in the quotes. Python
allows us to use single quotes, double quotes, or triple quotes to create the string.
Creating a string in Python
Syntax:
str = “Hi Python !”
Here, if we check the type of the variable str using a Python script
print(type(str)), then it will print a string (str).
In Python, strings are treated as the sequence of characters, which means that Python doesn’t support the
character data-type; instead, a single character written as ‘p’ is treated as the string of length 1.
#Using single quotes
str1 = ‘Hello Python’
print(str1)
#Using double quotes
str2 = “Hello Python”
print(str2)
#Using triple quotes
str3 = ‘’’Triple quotes are generally used for represent the multiline or docstring’’’
print(str3)
Output:
Hello Python
Hello Python
Triple quotes are generally used for represent the multiline or docstring.
Boolean
Booleans represent one of two values: True or False.
In programming you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two answers, True or False.
When you compare two values, the expression is evaluated and Python returns the Boolean answer:
print(10 > 9)
print(10 == 9)
print(10 < 9)
When you run a condition in an if statement, Python returns True or False:
Example:
Print a message based on whether the condition is True or False:
a = 200
b = 33
if b > a:
print(“b is greater than a”)
414
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137