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

Lecture8 Strings

The document provides an overview of string processing in Python, covering basic string methods, string traversal, and advanced string processing techniques. It explains how strings are treated as arrays of characters, discusses various string methods for searching, modifying, and formatting strings, and includes examples of string operations. Additionally, it highlights the immutability of strings and introduces escape characters for handling special characters within strings.

Uploaded by

2024880774.prem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture8 Strings

The document provides an overview of string processing in Python, covering basic string methods, string traversal, and advanced string processing techniques. It explains how strings are treated as arrays of characters, discusses various string methods for searching, modifying, and formatting strings, and includes examples of string operations. Additionally, it highlights the immutability of strings and introduces escape characters for handling special characters within strings.

Uploaded by

2024880774.prem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

THIS TOPIC IS FOR SELF STUDY

Refer these slides and


https://www.w3schools.com/python/python_strings.asp
for self study.
Lecture Contents
• String Processing:
• String

• Basic String methods

• String Traversal

• Sequence Operation in String

• Advanced String Processing


String Processing

• String processing refers to the operations performed on strings that allow them
to be accessed, analyzed, and updated.

• We have already seen some operations on strings—for example, str[k], for


accessing individual characters, and len(str) for getting the length of a
string.

• Python provides several other methods for string processing.


Strin
g String
• literals in python are surrounded by either single quotation marks, or
double quotation marks. Ex. 'hello' is the same as "hello".
• You can display a string literal with the print() function:
Example: Output:
print("Hello") Hello
print('Hello') Hello

• Assigning a string to a variable is done with the variable name followed by an


equal sign and the string. For multi line string use three double quotes or
three single quotes.
# Assigning Multi line string
# Single Word or single line string a = “”” Multi line string can be assigned
a = "Hello" using three double quotes, or
print(a) three single quotes“”"
print(a)
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.

Example: Get the character at position 1 # Example: String Slicing


(remember that the first character has the position 0):
b = "Hello, World!"
print(b[2:5])
a = "Hello, World!" Output: e
print(a[1]) Output: llo

• Slicing: You can return a range of characters by using the slice syntax.
• Specify the start index and the end index (excluded), separated by a colon, to
return a part of the string.
Strings are Arrays
• Use negative indexes to start the slice from the end of the string:

#Example
#Get the characters from position 5 to position 2 (not Output:
included), starting the count from the end of the string:
orl
b = "Hello,
World!" print(b[-
5:-2])
• String Length: To get the length of a string, use the len() function. This function
returns the length of the string.
Example: Output:

a = "Hello, 13
World!"
String Traversal
• The characters in a string can be easily traversed, without the use of an explicit
index variable, using the for chr in string form of the for statement.

Example1: Use of Index Variable Example2: Without using Index Variable


String-Applicable Sequence
Operations
• Since strings (unlike lists) are immutable, sequence-modifying operations are not
applicable to strings.
• For example: one cannot add, delete, or replace characters of a string.
• All string operations that “modify” a string return a new string that is a modified
version of the original string.
Applicable Sequence Operations:
Example

Note: The find, replace, and strip methods in Python can be used to search and
produce modified strings.
String Methods
Python provides a number of methods specific to strings, in addition to the
general sequence operations.
• Checking the Contents of a String
• Searching and Modifying Strings:
• Searching the contents of a String
• Replacing the contents of a String
• Removing the Contents of a String
• Splitting a String
Checking the Contents of a String
Searchi
ng,
Modifyi
ng and
Splittin
g
Strings
String Methods: Examples
• The strip() method: removes any whitespace from the beginning or the
end:
# Deleting a String using of
Example: Output: del
a = " Hello, World! " A =“hello”
print(a.strip()) Hello, World! del a
print(a
)

• The lower() and upper() methods returns the string in lower and upper case
respectively:
Example: Output:
a = "Hello,
World!" hello, world!
print(a.lower()) HELLO, WORLD!
print(a.upper())
String Methods: Examples
• The replace() method replaces a string with another
string:
Example: Output:
a = "Hello, World!"
print(a.replace("H", Jello, World!
"J"))

• The split() method splits the string into substrings if it finds instances of the separator:

Example: Output:
a = "Hello, World!"
print(a.split(",")) ['Hello', ' World!']
Other Check String: Examples
• To check if a certain phrase or character is present in a string, we can use the
keywords in or not in.

Example1:
#Check if the phrase "ain" is present in the following text: Output:
True
txt = "The rain in Spain stays mainly in the plain"
x = "ain" in txt
print(x)

Example2:
#Check if the phrase "ain" is NOT present in the following text:
Output:
False
txt = "The rain in Spain stays mainly in the plain"
x = "ain" not in txt
print(x)
String
Concatenation
• To concatenate, or combine, two strings you can use the + operator.
Example 1:
#Merge variable a with variable b into variable c:
Output:
a = "Hello"
HelloWorld
b = "World"
c = a + b
print(c)

Example 2:
#To add a space between them, add a " ": Output:

a = "Hello" Hello
b = "World" World
c = a + " " +
b print(c)
String Format
• We cannot combine strings and numbers.
Output:
Example:
age = 36
txt = "My name is John, I am " +
age print(txt)

• Python provides format() method to combine strings and numbers.


• 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:
Output:
age = 36
txt = "My name is John, and I am {}" My name is John, and I am
print(txt.format(age)) 36
String Format: Named Indexes
• You can also use named indexes by entering a name inside the curly
brackets {carname}.

• In this case, you must use names when you pass the parameter
values txt.format(carname = "Ford"):

Example:

myorder = "I have a {carname}, it is a {model}."


print(myorder.format(carname = "Ford", model =
"Mustang"))

Output:
I have a Ford, it is a Mustang.
String Format: Example
• The format() method takes unlimited number of arguments, and are placed into
the respective placeholders:
Example: Output:
quantity = I want 3 pieces of item 567 for
3 49.95 dollars.
itemno==49.95
price
567
myorder = "I want {} pieces of item {} for {}
dollars." print(myorder.format(quantity, itemno,
price))
• You can use index numbers {0} to be sure the arguments are placed in the correct
placeholders:
Example: Output:
quantity = I want to pay 49.95 dollars
3 for
itemno==49.95
price 3 pieces of item 567
567
myorder = "I want to pay {2} dollars for {0} pieces of item
{1}." print(myorder.format(quantity, itemno, price))
• To insert characters that are illegal in a string, use an
escape character.
• An escape character is a backslash \ followed by the
character you want to insert.
• An example of an illegal character is a double quote inside
a string that is surrounded by double quotes:

Escape Example:
#You will get an error if you use double quotes inside a string
Charact that is surrounded by double quotes:

er txt = "We are the so-called "Vikings" from the


north."
Output:
Escape Character: Example
• To fix this problem, use the escape character \":

• The escape character allows you to use double quotes when you
normally would not be allowed:

Example:

txt = "We are the so-called \"Vikings\" from the


north."
Output:

We are the so-called "Vikings" from the


north.
Other Escape Characters used
Code in Python
Result Example Output
\' Single Quote print('It\'s alright.') It's alright.

\\ Backslash print("This will insert one \\ (backslash).“) This will insert one \ (backslash).

Hello
\n New Line print("Hello\nWorld!“) World!
Hello
\r Carriage Return print("Hello\rWorld!“) World!
\t Tab print("Hello\tWorld!“) Hello World!
\b Backspace print("Hello \bWorld!“) HelloWorld!
#A backslash followed by three integers will result in
\ooo Octal value a octal value: Hello
print("\110\145\154\154\157“)
#A backslash followed by an 'x' and a hex number
\xhh Hex value represents a hex value: print("\x48\x65\x6c\x6c\ Hello
x6f“)
MCQ
1.
s on, while others return a new altered
Some string methods alter the string they are
called
4. Indicate which of the following is true.
a) String method isdigit returns
version of the string. true if the
a) TRUE string applied to contains any digits.
b) FALSE b) String method isdigit returns
true if the string applied to
contains only digits.
2. The find method returns the number of
occurrences of a character or substring within
a given string. 5. Indicate which of the following s.replace('c','e’)
a) TRUE returns for s = 'abcabc’.
b) FALSE a) 'abeabc’ b) 'abeabe’

3. Which of the results below does s[2:4] return 6. Which of the results below does
for the string s = 'abcdef’. s.strip(‘-’)
a) 'cd’ b) 'bcd’ c) 'bc’ d) 'cde' return for the string s = ‘---ERROR---’.
a) '---ERROR’ b) 'ERROR---’ c)
'ERROR'
MCQs:
1.
Answers
Some string methods alter the string they are
called on, while others return a new altered
4. Indicate which of the following is true.
a) String method isdigit returns
version of the string. true if the
a) TRUE string applied to contains any digits.
b) FALSE b) String method isdigit returns true if the
string applied to contains only digits.
2. The find method returns the number of
occurrences of a character or substring within 5. Indicate which of the following s.replace('c','e’)
a given string. returns for s = 'abcabc’.
a) TRUE a) 'abeabc’ b) 'abeabe’
b) FALSE
6. Which of the results below does
3. Which of the results below does s[2:4] return s.strip(‘-’)
for the string s = 'abcdef’. return for the string s = ‘---ERROR---’.
a) 'cd’ b) 'bcd’ c) 'bc’ d) 'cde' a) '---ERROR’ b) 'ERROR---’ c)
'ERROR'

You might also like