Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Module 1 - String

Uploaded by

nachiketasvarma6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module 1 - String

Uploaded by

nachiketasvarma6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

ALGORITHMIC THINKING

WITH PYTHON
Strings
 A string is a sequence of zero or more characters.
 Each character in the string occupies one byte of memory.
 Last character is always \0 (null character) – indicates the end
of the string.
 Every character in the string is numbered or indexed (null
character is not numbered since it only indicates the end of
the string).
 Empty string can be created in order to initialise a variable.
 Sample indexing of the string “Hello World” is provided in the
figure.
 len() – to know the length of the string
String – subscript operator
 To print a single character out of the string
 Also called bracket operator
 Python allows negative subscript values
 [ ] operator cannot be used on the left side of an assignment
operator to change the character of a string.

Strings are immutable


String – Operations - Concatenation
 Concatenation – combines strings using + operator

 Concatenation does not insert any white space between the


joined string
String – Operations - Repetition
 Repetition – to repeat strings using * operator

>>> a = ‘fruit’
>>> c = 3
>>> r = a * c
>>> print(r)
fruitfruitfruit
String – Operations -Slicing
 Slicing – to extract a substring out of a given string
 The operator [n:m] returns part of the string starting a
position n and ending at position m-1

>>> greet = “Hello world of python”


>>> print(greet[0:4]) # Output : Hell
>>> print(greet[6:11]) # Output : world
>>> print(greet[15:21]) # Output : python
>>> print(greet[ :6]) # Output : Hello
>>> print(greet[15: ]) # Output : python
>>> print(greet[ : ]) # Output : Hello world of python
Slicing - contd
 In addition to mentioning start and end indices, we can also
specify the step that denotes the distance between the
characters.
#To print every alternate character in the resultant string
>>> language = “Python, Java and C++”
>>> print(language[ : :2])
Pto,Jv n +

>>> a = “Emilin”
>>> print(a[ : :-1])
‘nilimE’ #Starts at the end and walks backward
String Comparison
 Used to compare strings
String – in operator
 in is a Boolean operator that takes two strings and returns
True if the first appears as a substring in the second and False
otherwise.
String module
Traversing a string
 To process one character at a time
 From left end to right end
 Using while loop:  Using for loop:

You might also like