Introduction Variables Data Types Numbers Casting Strings Boolean Operators Lists Tuples Dictionaries If-Else While Loops For Loops Functions Arrays Scope Modules User Input File Handling Python Maths

Welcome to the lesson on Python Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks.

'hello' is the same as "hello"

You can display a string literal by using print()

Some Examples:

syntax
Try it out yourself

Assigning String to a variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

Some Examples:

syntax
Try it out yourself

Multiline Strings

You can assign a multiline string to a variable by using three quotes:

Some Examples: three double quotes

syntax

Some Examples: three single quotes

syntax
Try it out yourself

Strings are Arrays

strings in Python are arrays of bytes representing unicode characters.

Some Examples: Get the character at position 7 (remember that the first character has the position 0):

syntax
Try it out yourself

Looping Through a String

Since strings are arrays, we can loop through the characters in a string, with a for loop.

Some Examples: Loop through the letters in the word "Grape":

syntax
Try it out yourself

String Length

To get the length of a string, use the len() function.

Some Examples: The len() function returns the length of a string:

syntax
Try it out yourself

Check String

To check if a certain phrase or character is present in a string, we can use the keyword in

Some Examples: Check if "movie" is present in the following text:

syntax
Try it out yourself

Some Examples: Using an if statement:

syntax
Try it out yourself

Check string not in a variable

To check if a certain phrase or character is NOT present in a string, we can use the keyword not in

Some Examples: Check if "Singer" is NOT present in the following text:

syntax
Try it out yourself

Some Examples: Using an if statement:

syntax
Try it out yourself

End of Python Strings

You have learned Python Strings in simple terms. Let's proceed on to Slicing Strings.

Next
Back To Top