0% found this document useful (0 votes)
4 views

Python

The document discusses various string operations in Python including: 1. List comprehension can be used to reverse the order of elements in a string and join them. 2. Strings are immutable in Python, so individual characters cannot be updated or deleted, only entire strings. Escape sequences allow representing special characters in strings. 3. String formatting allows combining variables and values to create dynamic strings using methods like the % operator or format() function.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python

The document discusses various string operations in Python including: 1. List comprehension can be used to reverse the order of elements in a string and join them. 2. Strings are immutable in Python, so individual characters cannot be updated or deleted, only entire strings. Escape sequences allow representing special characters in strings. 3. String formatting allows combining variables and values to create dynamic strings using methods like the % operator or format() function.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

5.

Using List Comprehension


List comprehension creates the list of elements of a string in reverse order and then its elements
are joined using join(). And reversed order string is formed.
Example:

DELETING/ UPDATING FROM A STRING


In Python, the Updation or deletion of characters from a String is not allowed. This will cause
an error because item assignment or item deletion from a String is not supported. Although
deletion of the entire String is possible with the use of a built-in del keyword. This is because
Strings are immutable, hence elements of a String cannot be changed once assigned. Only new
strings can be reassigned to the same name.
Updating a Character
Example:

Updating the entire string


Example:
Deletion of a Character
Example:

Deletion of the entire String


Deletion of entire string is possible with the use of del keyword. Further, if we try to print the
string, this will produce an error because String is deleted and is unavailable to be printed.
Example:
ESCAPE SEQUENCING
An escape sequence is a special character used in the form of backslash(\) followed by a
character that is required. These characters are used to represent whitespace. Whitespace gives
characters like space, tab, formfeed, and vertical tab.
The following are the some of the common Escape Sequences in Python.
Escape Sequence Description
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\n Newline
\t Tab
\r Carriage return
\b Backspace
\f Form feed
\ooo Character with octal value ooo
\xhh Character with hex value hh
▪ Escape sequence for single and double quotes: While printing Strings with single and
double quotes in it causes SyntaxError because String already contains Single and
Double Quotes and hence cannot be printed with the use of either of these. Hence, to
print such a String either Triple Quotes are used or Escape sequences are used to print
such Strings.
Syntax: The characters need to be preceded by a backslash character
Example:

Python searches for an ending-single quote in the string 'I'm a "Writer" '. Hence, it raises an
error. Below is an example of escape single and double quotes in Python using \’ single quote
and \” in strings in Python.
Example:

▪ Escape sequence n: Let us see an example of Python escape sequence n. A newline


character is used to write the words in a new separate line.
Example:
The above code prints the words in a new separate line.
▪ Escape sequence backslash: The backslash is an escape sequence, \\ is used to print a
single backslash.
Example:

▪ Escape sequence for space \t: The “\t” character is used to get space between the words.
Example:

Here, we can see the space between the word Python and Programming.
▪ Escape sequence backspace \b: Using “\b” removes the space between the words in
Python.
Example:

The above code uses \b to remove space before Programming.


▪ Escape sequence for hexa value: The \xhh converts hexa value into a string.
Example:

The above code outputs the converted string.


▪ Raw Strings and Escape Sequences: In Python, we can create raw strings by prefixing
the string with the letter r or R. In raw strings, backslashes are treated as literal
characters and not as escape characters in Python.
Example:

FORMATTING STRINGS
String formatting in Python allows us to create dynamic strings by combining variables and
values.
▪ Formatting string using % Operator: The modulo % is also known as the “string-
formatting operator”.

Injecting Multiple Strings using the modulo Operator


x = 'looked'
print("Misha %s and %s around"%('walked',x))

Output:
Misha walked and looked around.

You might also like