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

4.7.Python String

The document provides a comprehensive overview of strings in Python, detailing their definition, immutability, and indexing methods. It covers basic string operators such as concatenation, replication, and membership, along with the ord() and chr() functions. Additionally, it explains string slicing and includes examples of using for loops with strings and built-in string functions.

Uploaded by

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

4.7.Python String

The document provides a comprehensive overview of strings in Python, detailing their definition, immutability, and indexing methods. It covers basic string operators such as concatenation, replication, and membership, along with the ord() and chr() functions. Additionally, it explains string slicing and includes examples of using for loops with strings and built-in string functions.

Uploaded by

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

Table of contents

STRING IN PYTHON
Basics of String:
Basic String Operators
a) String Concatenation Operator (+)
b) String Replication Operator (*)
C) Membership Operators: (in & not in)
D) ord() & chr() functions
F) String Slice :
for loop with string
Built in functions in String

STRING IN PYTHON
Definition: String is a collection of characters. Strings can be created by enclosing characters
inside a single quote or double-quotes. Even triple quotes can be used in Python but
generally used to represent multiline strings. Python does not have a character data type, a
single character is simply a string with a length of 1.

Basics of String:
Strings are immutable means that the contents of the string cannot be changed after it is
created. At the same memory address, the new value cannot be stored. Python does not
allow the programmer to change a character in a string.

Example:

str=‘jaipur’

strTypeError: ‘str’ object does not support item assignment .

As shown in the above example.As shown in the above example, str has the value “jaipur”.
An attempt to replace ‘j’ in the string by ‟J‟ displays a TypeError.

Each character has its index or can be accessed using its index. String in python has two-way
index for each location. (0, 1, 2, ……. In the forward direction and -1, -2, -3,……… in the
backward direction.)

Important points about accessing elements in the strings using subscript(INDEXING) Positive
subscript helps in accessing the string from the beginning Negative subscript helps in
accessing the string from the end. Subscript 0 or –ve n(where n is length of the string)
displays the first element. Example : A[0] or A[-5] will display “H” Subscript 1 or –ve (n-1)
displays the second element.

Basic String Operators

a) String Concatenation Operator (+)


The + operator creates a new string by joining the operand strings. e.g.

“Hello” + “Students”

will result into : “HelloStudents”

‘11’ + ‘22’ will give: ‘1122’

“a” + “5” will give : “a5”

‘123’ + ‘abc’ will give: ’123abc‘

“a” + 5 is invalid

•Note: both operands must be strings.

b) String Replication Operator (*)

The ‘*’ operator when used with String, then it creates a new string that is a number of
replications of the input string. e.g.

“XY” * 3 will give: “XYXYXY”

“3” * 4 will give: "3333“

“5” * “7” is invalid

Note: operands must be one string & other Number

C) Membership Operators: (in & not in)

in : returns True if a character or a substring exists in the given string, False otherwise.

“a” in “Rajat” gives: True “Raj” in “Rajat” gives: True “67” in “1234” gives: False

not in: returns True if a character or a substring does not exists in the given string, False
otherwise. “a” not in “Rajat” gives: False "Raj“ not in “Rajat” gives: False “67” not in “1234”
gives: True

D) ord() & chr() functions

ord() function: It gives ASCII value of a single character ord (“a”) : 97 ord (“Z”) : 90 ord(“0”) :
48 chr() function: it is opposite of ord() function chr(97) : “a" chr(90) : “Z" chr(48) : “0”

F) String Slice :

Extracting a subpart from a main string is called slicing .It is done by using a range of indices.
Syntax:

string-name[start:stop:step]

Note: it will return a string from the index start to stop-1. Example:

s=“TEACHER”
0

-7

-6

-5

-4

-3

-2

-1

s[2:6:1] ‘ACHE’

s[6:1:-1] ‘REHCA’

s[0:10:2] ‘TAHR’

s[-8:-3:1] ’TEAC

s[ : 6 : 1] # Missing index at start is considered as 0. ‘TEACHE’

s[2 : :2] # Missing index at stop is considered as last index. ‘AHR’

s[3:6: ] # Missing index at step is considered as 1. ‘CHE’

s[ : :-1] ‘REHCAET’
s[2: :]+s[ :2 :] ‘ACHERTE’

s[1: 5:-1]‘ ‘

Let word=“WelCome” Then, word [0:7] will give : WelCome word [0:3] will give : Wel word
[2:5] will give : lCo (2,3,4) word [-6:-3] will give : elC (-6,-5,-4) word [ :5] will give : WelCo
(0,1,2,3,4) word [3: ] will give : Come (5,6,7,8)

for loop with string

e.g.1:

for ch in “Hello” :
print(ch)

Output: H e l l o

Built in functions in String

Below listed the built in functions for String manipulation.

You might also like