Session 4

 Strings:

The string is a data type in Python.

A string is a sequence of characters enclosed in quotes.

We can primarily write a string in three ways:

  1. Single quoted strings : a = ‘Time’
  2. Double quoted strings : b =  “Hey”
  3. Triple quoted strings : c = ‘’’ Mustajab‘’’

String Slicing:

A string in Python can be sliced for getting a part of the string. The index in a string starts from 0 to (length-1) in Python. To slice a string, we use the following syntax:

s1= name[ind_start: ind_end]

Example:

b = "Mustajab Ali"
print(b[2:5])

Negative Indices: 

Using a negative number as an index in the slice method is called Negative slicing in Python. It returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).Negative indices can also be used as shown in the above example. -1 corresponds to the (length-1) index, -2 to (length-2).

Example:

mystr = "Hello" print(mystr[-2:])

Slicing with skip value

We can provide a skip value as a part of our slice like this:


word = “amazing”

word[1:6:2]           # It will return ’mzn’


Other advanced slicing techniques:


word = ‘amazing’

word[:7] or word[0:7]      #It will return ‘amazing’

word[0:] or word[0:7]      #It will return ‘amazing’


String Function:

Some of the most used functions to perform operations on or manipulate strings are:

  1. len() function : It returns the length of the string.
  2. endswith(“jab”) : This function tells whether the variable string ends with the string “jab” or not. If string is “mustajab”, it returns for “jab” since harry ends with jab.
  3. count(“c”) : It counts the total number of occurrences of any character.
  4. capitalize() : This function capitalizes the first character of a given string.
  5. find(word) : This function finds a word and returns the index of first occurrence of that word in the string.
  6. replace(oldword, newword) : This function replaces the old word with the new word in the entire string.

Escape Sequence Characters:

Sequence of characters after backslash ‘\’ [Escape Sequence Characters]

Escape Sequence Characters comprises of more than one character but represents one character when used within the string.

Examples: \n (new line), \t (tab), \’ (single quote), \\ (backslash), etc.




Practice Task:

  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|>
  1. Write a program to detect double spaces in a string.
  2. Replace the double spaces from problem 3 with single spaces.
  3. Write a program to format the following letter using escape sequence characters.
letter = “Dear Fellows, This Python course in nice. Thanks!!”



Comments

Popular posts from this blog

Loops and Functions Session 8

Python Installation

How to Change your (dev/visual) console color