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

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

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

Joining the string using concatenation

String Methods

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

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

To find a sequence of character from a string

To replace a character or sequence of character from a string

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

Numerical Data Type

Arithmetic Operators

  • While assigning values to any variable, no need to declare the variable in advance

  • Reassigning a different type to the same variable is possible

Working with numbers

circle-check

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

Boolean Data Type

It takes only two values, True or False

Last updated

Was this helpful?