Python Strings
In Python, strings are a fundamental data type used to represent text. You can create strings by enclosing text in either single or double quotation marks. For example, 'hello'
and "hello"
are both valid string representations, and they are considered equivalent.
Displaying Strings
You can display a string literal using the print()
function. Here's an example:
Assigning Strings to Variables
To store a string in a variable, you simply use the variable name followed by an equal sign and the string itself. For instance:
Multiline Strings
Python allows you to work with multiline strings easily by using triple quotes. You can use either triple double quotes or triple single quotes to create multiline strings. For example:
or
Strings as Arrays
Strings in Python are treated as arrays of bytes representing Unicode characters. Unlike some other programming languages, Python doesn't have a character data type. Instead, a single character is represented as a string with a length of 1. You can access individual characters in a string using square brackets, and remember that indexing starts from 0.
Looping Through a String
You can loop through the characters of a string using a for
loop:
String Length
To determine the length of a string, you can use the len()
function. For example:
Checking for Substrings
To check if a specific word or character is present in a string, you can use the in
keyword. For instance:
You can also use the not in
keyword to check if a word or character is NOT present in a string:
These string operations allow you to manipulate and search for data within strings, making Python a versatile language for text processing.
Last updated