Primitive Data Types

String Data Type

Understanding the use of single', double" and triple''' quote

How to get the length of a string variable?

There is no difference in python with either single or double quotes, we can use any of the two for declaring the variables.

course = "Python Programming"
course = 'Python Programming'

course = 'Python" Developer' # (here variable is Python" Developer)

The triple quote is used for multiline string declaration or using " and ' within variables (in case don't want to use escape character)

course = ''' this is multiline string declaration
and everything within triple quotes will be stored within
a single variable.
'''

How to get the length of a string variable?

We can use the inbuilt len() function to get the length of the string variable. A function is a piece of reusable code to perform a certain task, we will discuss it in detail in a later section.

len("what is your name") # it will return 17 as the length

How to access a specific character in a string?

  • Strings in Python are zero-indexed (the first letter will be indexed to 0)

  • Index 0 returns the first character of a string Index

  • -1 returns the first character of a string from the end

course = "Python Programming"
print(course[3]) # prints h
print(course[-2]) # prints n

How to slice the string?

We can slice a string by varible_name[start_index:end_index] Note that the character at the end_index is not included in the result

print(course[0:3]) #returns a new string (Pyt) with first 3 characters from the beginning

Escape Character(\) and Escape Sequences(" , ' , \\ , \n)

A backslash(\) in python is a special character called escape character and the combination of \" is an Escape sequence

# Listed below are some other Escape sequences

course = "Python \"Programming"
print(course) # prints Python \"Programming

course = "Python \'Programming"
print(course) # prints Python 'Programming

course = "Python \\Programming"
print(course) # prints Python \Programming

course = "Python \nProgramming"
print(course) # prints Python and Programming in separate lines

Joining the string using concatenation

first = "OoBA"
last = "Labs"
full = first + " " + last
print(full) # prints # OoBA Labs

String Methods

Convert string to upper case, lower case, and capitalize the first letter of every word

course = "pyThon prograMming"
print(course.upper()) # prints PYTHON PROGRAMMING
print(course.lower()) # prints python programming
print(course.title()) # prints Python Programming, note T and M are small as well

Trim any white space at the beginning or end of the string or from both end and start

course = "  |;  Python, Programming,  "

print(course.strip(' ,|;')) 
# it will strip whatever mentioned in quotes
# prints Python, Programming (without spaces and ,;|)

print(course.lstrip()) # strips spaces only at the left corner
print(course.rstrip()) # strips spaces only at the right corner

To find a sequence of character from a string

course = "Python Programming"
print(course.find("pro")) ## It will return -1 as pro is not present in the string
print(course.find("Pro")) ## It will return the index of Pro ie is 7

To replace a character or sequence of character from a string

course = "Python Programming"
print(course.replace("P" , "J")) # print Jython Jrogramming

To find the existence of character or sequence of character in a string

course = "Python Programming"
print("Pro" in course) # prints True
print("pro" not in course) # prints True because Pro is present not pro

Numerical Data Type

Arithmetic Operators

print(a + b) #It will add a and b
print(a - b) #It will substract b from a 
print(a * b) #It will multiply a and b
print(a / b) #It will return a float value in python3, even if a and b both are int
print(a // b) #It will return an integer value in python3
print(a % b) #It will return remainder of the division
print(a ** b) #It will return a the power b
  • While assigning values to any variable, no need to declare the variable in advance

  • Reassigning a different type to the same variable is possible

# Augmented assignment operator

x = 10
x = x + 3
x += 3 

# similarly others
x*=3
x-=3
x/=3

Working with numbers

# Rounding a number
print(round(2.9))

# Absolute value of a number
print(abs(-2.9))


# math module for more functions
import math
print(math.ceil(2.3))

Variable Names - How to write cleaner code?

  • The print statement works to print (especially very important in debugging the code)

    *debug = determine the cause of an error

  • 5 and '5' are not equal or same, 5 is of integer type and '5' is of string type

  • Variable casting is possible (changing the variable type from one another)

Typecasting

meaning changing the type of the variable from string to integer (possible only if the number is within string otherwise throws an error), or any other variable

x = input("x: ")
y = int(x) + 5
print(y)

print(int(x)) # try it yourself
print(float(x))
print(bool(x)) 
print(str(x))

Boolean Data Type

It takes only two values, True or False

# Following results in False Boolean, all else comes out True

print(bool("")) 
print(bool(0)) 
print(bool(None)) 
print(bool([]))

Last updated

Was this helpful?