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

Day3 Python

Chapter 3 covers the string data type in Python, explaining how to create strings using single, double, and triple quotes. It discusses string slicing, including the use of negative indices and skip values, as well as common string functions like len(), endswith(), count(), capitalize(), find(), and replace(). The chapter concludes with a practice set of exercises to reinforce the concepts learned.

Uploaded by

vikramsen7974
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Day3 Python

Chapter 3 covers the string data type in Python, explaining how to create strings using single, double, and triple quotes. It discusses string slicing, including the use of negative indices and skip values, as well as common string functions like len(), endswith(), count(), capitalize(), find(), and replace(). The chapter concludes with a practice set of exercises to reinforce the concepts learned.

Uploaded by

vikramsen7974
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 3 – STRINGS

String is a data type in python.

String is a sequence of characters enclosed in quotes.

We can primarily write a string in these three ways.

a ='harry' # Single quoted string


b = "harry" # Double quoted string
c = '''harry''' # Triple quoted string

STRING SLICING
A string in python can be sliced for getting a part of the strings.

Consider the following string:

The index in a sting starts from 0 to (length -1) in Python. In order to slice a string, we use
the following syntax:

Negative Indices: Negative indices can also be used as shown in the figure above. -1
corresponds to the (length - 1) index, -2 to (length - 2).

13
SLICING WITH SKIP VALUE
We can provide a skip value as a part of our slice like this:

word = "amazing"

word[1: 6: 2] # "mzn"

Other advanced slicing techniques:

Word = "amazing"
Word = [:7] # word [0:7] – 'amazing'
Word = [0:] # word [0:7] – 'amazing'

STRING FUNCTIONS
Some of the commonly used functions to perform operations on or manipulate strings
are as follows. Let us assume there is a string ‘str’ as follows:

str = 'harry'
Now when operated on this string ‘str’, these functions do the following:

1. len () function – This function returns the length of the strings.

str = "harry"
print(len(str)) # Output: 5
2. String.endswith("rry") – This function_ tells whether the variable string ends with
the string "rry" or not. If string is "harry", it returns true for "rry" since Harry ends
with rry.

str = "harry"
print(str.endswith("rry")) # Output: True
3. string.count("c") – counts the total number of occurrences of any character.

str = "harry"
count = str.count("r")
print(count) # Output: 2
4. the first character of a given string.

str = "harry"
capitalized_string = str.capitalize()
print(capitalized_string) # Output: "Harry"
5. string.find(word) – This function friends a word and returns the index of first
occurrence of that word in the string.

str = "harry"

14
index = str.find("rr")
print(index) # Output: 2
6. string.replace (old word, new word ) – This function replace the old word with
new word in the entire string.

str = "harry"
replaced_string = str.replace("r", "l")
print(replaced_string) # Output: "hally"

ESCAPE SEQUENCE CHARACTERS


Sequence of characters after backslash "\" → Escape Sequence characters

Escape Sequence characters comprise of more than one character but represent one
character when used within the strings.

15
CHAPTER 3 – PRACTICE SET
1. Write a python program to display a user entered name followed by Good
Afternoon using input () function.
2. Write a program to fill in a letter template given below with name and date.
letter = '''
Dear <|Name|>,
You are selected!
<|Date|>
'''

3. Write a program to detect double space in a string.


4. Replace the double space from problem 3 with single spaces.
5. Write a program to format the following letter using escape sequence
characters.
letter = "Dear Harry, this python course is nice. Thanks!"

16

You might also like