Python Basic Syntax String
Python Basic Syntax String
STRING
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 with the print() function.
Example:
STRING
Assign String to a Variable
Assigning a string to a variable is done with the variable name
followed by an equal sign and the string:
Example:
STRING
Multiline Strings
You can assign a multiline string to a variable by using three
quotes.
Example:
STRING
Multiline Strings
Or three single quotes:
Example:
STRING
Strings are Arrays
Like many other popular programming languages, strings in
Python are arrays of bytes representing unicode characters.
However, Python does not have a character data type, a single
character is simply a string with a length of 1.
Square brackets can be used to access elements of the string.
STRING
Strings are Arrays
Example: Get the character at position 1 (remember that the first
character has the position 0).
STRING
Looping Through a String
Since strings are arrays, we can loop through the characters in a
string, with a for loop.
Example: Loop through the letters in the word "banana“.
The len() function returns the length of a string:
STRING
String Length
To get the length of a string, use the len() function.
Example: The len() function returns the length of a string.
STRING
Check String
To check if a certain phrase or character is present in a string, we
can use the keyword in.
Example: Check if "free" is present in the following text.
STRING
Check String
Use it in an if statement:
Example: Print only if "free" is present.
STRING
Check if NOT
To check if a certain phrase or character is NOT present in a
string, we can use the keyword not in.
Example: Check if "expensive" is NOT present in the following
text.
STRING
Use it in an if statement:
Example: print only if "expensive" is NOT present.
SLICING STRINGS
STRING
Slicing Strings
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon,
to return a part of the string.
Example: Get the characters from position 2 to position 5.
STRING
Slice From the Start
By leaving out the start index, the range will start at the first
character:
Example: Get the character from the start to position 5.
STRING
Slice To the End
By leaving out the end index, the range will go to the end:
Example: Get the characters from position 2, and all the way to
the end.
STRING
Negative Indexing
Use negative indexes to start the slice from the end of the string:
Example:
Get the characters:
From: "o" in "World!" (position -5)
To, but not included: "d" in "World!" (position -2).
MODIFY STRINGS
STRING
Python has a set of built-in methods that you can use on strings.
Upper Case
Example:
The upper() method returns the string in upper case.
STRING
Lower Case
Example:
The lower() method returns the string in lower case.
STRING
Replace String
Example: The replace() method replaces a string with another
string.
STRING
Split String
The split() method returns a list where the text between the
specified separator becomes the list items.
Example: The split() method splits the string into substrings if it
finds instances of the separator.
FORMAT STRINGS
STRING
String Format
The format() method takes the passed arguments, formats them,
and places them in the string where the placeholders {} are.
Example: Use the format() method to insert numbers into strings.
STRING
String Format
The format() method takes unlimited number of arguments, and
are placed into the respective placeholders.
Example:
STRING
String Format
You can use index numbers {0} to be sure the arguments are
placed in the correct placeholders.
Example:
ESCAPE CHARACTERS
STRING
Escape Characters
In Python strings, the backslash is a special character, also called
the “escape” character. It is used in representing certain
whitespace characters: “\t” is a tab, “\n” is a new line, and “\r” is a
carriage return. Finally, “ ” can be used to escape itself: “\” is the
literal backslash character.
STRING
Escape Characters
It is used to insert characters that are illegal in a string.
An escape character is a backslash \ followed by the character
you want to insert.
STRING
Escape Characters
An example of an illegal character is a double quote inside a
string that is surrounded by double quotes.
Example: You will get an error if you use double quotes inside a string
that is surrounded by double quotes:
STRING
Escape Characters
To fix this problem, use the escape character \":
Example: The escape character allows you to use double quotes
when you normally would not be allowed.
STRING
Other escape characters used in Python:
STRING METHODS
STRING METHODS
Python has a set of built-in methods that you can use on strings.
STRING METHODS
STRING METHODS
STRING METHODS