Index
- Introduction
- raw-string
- aritmetic operators
- comparison operators
- index
- slicing
- loops
- Built-in functions
Introduction
Strings main characteristics:
-
Contained by quotes: Strings are defined within single(’) or double quotes(”).
-
Sequence of characters: A string is a sequence of characters, meaning they can contain text characters, numbers, special symbols, and more.
-
Iterable Object: can be divided in samller parts
-
Immutable:* a string is an immutable object, which means that once you create a string object, you cannot modify its contents.
-
Ordinated: admits the repetition of elements and can be sliced and indexed
raw-string
Python Raw String
read: Python Backslash (escaping character) and Python Escaping Sequences
Definition
In Python, when you prefix a string with the letter
r
orR
such asr'...'
andR'...'
, that string becomes a raw string. Unlike a regular string, a raw string treats the backslashes (\
) as literal characters.Raw strings are useful when you deal with strings that have many backslashes, for example directory paths on Windows.
Example:
Output:
Output: lang ver Python 3
However, raw strings treat the backslash (
\
) as a literal character. For example:
Convert a regular string into a raw string
To convert a regular string into a raw string, you use the built-in repr() function. For example:
Link to original
Concatenation and Replication
Note:
A string is an immutable object, which means that when you concatenate two strings you are creating a new one
Operator | Description | Example |
---|---|---|
+ | Concatenate two strings | ”hello” + “world” → “hello world” |
* | Repeat a string for a certain number of times | ”spam” * 3 → “spamspamspam” |
Comparison Operators
In Python, you can compare two strings using comparison operators. The comparison operators return a Boolean value (True
or False
) depending on whether the comparison is true or false.
The comparison operators available for strings are:
==
(equal to): returnsTrue
if the two strings are equal, andFalse
otherwise.!=
(not equal to): returnsTrue
if the two strings are not equal, andFalse
otherwise.<
(less than): returnsTrue
if the first string comes before the second string in alphabetical order, andFalse
otherwise.>
(greater than): returnsTrue
if the first string comes after the second string in alphabetical order, andFalse
otherwise.<=
(less than or equal to): returnsTrue
if the first string comes before or is equal to the second string in alphabetical order, andFalse
otherwise.>=
(greater than or equal to): returnsTrue
if the first string comes after or is equal to the second string in alphabetical order, andFalse
otherwise.in
: returnsTrue
if a substring is found within a string, andFalse
otherwise.not in
: returnsTrue
if a substring is not found within a string, andFalse
otherwise.
Here are some examples:
Note:
Note that when comparing strings, Python uses lexicographical order, which means it compares the ASCII values of the characters in the strings.
Built-in functions
Python offers numerous built-in functions for working with strings. Here are some of the most commonly used functions:
len()
The len()
function returns the length of the string. For example:
str()
The str()
function converts a value to a string. For example:
my_number = 42
my_string = str(my_number)
print(my_string) # Output: "42"
count()
The count()
function counts the number of occurrences of a substring within a string. For example:
my_string = "Hello, World!"
print(my_string.count("l")) # Output: 3
join()
The join()
function joins a list of strings into a single string using a specific separator. For example:
split()
The split()
function splits a string into a list of strings using a specific separator. For example:
upper()
and lower()
The upper()
and lower()
functions convert a string to uppercase or lowercase letters. For example:
islower()
and isupper()
The islower()
and isupper()
functions return True if all characters in the string are lowercase or uppercase respectively, otherwise False. For example:
capitalize()
The capitalize()
function converts the first letter of a string to uppercase. For example:
swapcase()
The swapcase()
function converts uppercase letters to lowercase and vice versa within a string. For example:
replace()
The replace()
function replaces a substring with another substring within a string. For example:
find()
The find()
function returns the index of the first occurrence of a substring within a string, or -1 if the substring is not found. For example:
title()
The title()
function converts the first letter of each word to uppercase within a string. For example:
startswith()
and endswith()
The startswith()
and endswith()
functions check if a string starts or ends with a certain substring and return True or False. For example:
format()
The format()
function formats a string by inserting values into specified positional or named placeholders. For example:
replace()
The replace()
function replaces a substring with another one within a string. For example:
Slicingof a string
Note:
Slicing a string does not modify the original string - it returns a new string that contains only the specified portion of the original string, because strings are immutable
You can slice a string in Python using the syntax string[start:end:step]
, where:
start
is the index of the first character you want to include in the sliceend
is the index of the first character you want to exclude from the slicestep
is the number of characters to skip between each character included in the slice
Here are some examples:
Note:
That you can mix positive and negative indices in a single slice, and you can omit any of the three parameters (
start
,end
, andstep
) as needed.
Quartz Index Example of a string
In Python, a string is a sequence of characters, and you can access individual characters in the string by their position or index within the sequence.
- The index of a string starts at 0, which means that the first character in the string has an index of 0
- The second character has an index of 1, and so on.
To access a specific character in a string, you can use its index within square brackets []
, like this:
In this example, we access each character in the string string
by its index within square brackets []
.
Note:
The index must be an integer, and it must be within the range of valid indices for the string (from 0 to
len(string) - 1
). Otherwise, aIndexError
will be raised.
Loops in a string
In Python, a loop is a programming construct that allows you to iterate over a sequence of items, such as the characters in a string.
There are two types of loops in Python: the for
loop and the while
loop.
-
we use a
for
loop to iterate over each character in the string. -
while
loop is designed to repeat a block of code while a specific condition is true