0% found this document useful (0 votes)
4 views2 pages

Python 7

The document explains the string data structure in programming, highlighting its definition, properties, and methods of accessing elements. Strings are ordered, allow duplicates, support both positive and negative indexing, and are immutable. Examples demonstrate string creation, indexing, and the type of errors that can occur when attempting to modify strings.

Uploaded by

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

Python 7

The document explains the string data structure in programming, highlighting its definition, properties, and methods of accessing elements. Strings are ordered, allow duplicates, support both positive and negative indexing, and are immutable. Examples demonstrate string creation, indexing, and the type of errors that can occur when attempting to modify strings.

Uploaded by

lnk313677
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Collection Data Structure

09 November 2025 01:22 PM

String String is defined by using singel,double or triple quatations(',",''')


Single line strings are created by using singel quatations or double quatations(','')
Multiline strings are created using triple quatations(''')

String is ordered Data Type and Allows Duplicates

String supports index ,index is of 2 types 1)positive index


2)Negative index

1)positive index starts from 0 at starting of the string.


2)Negative index starts from -1,at ending of the string

-5 -4 -3 -2 -1 ←-ve
s=h e l l o
+ve→ 0 1 2 3 4

To get a item from a string (syntax:


variabelName[index])
>>>s[2]
l
String is homogenious Data Type(same Type of data)
String is Immutable Data Type(the data can not be modified or changed)

>>>s='hello'
>>>type(s)
<class 'str'>
>>>s1="hello"
>>>len(s)
1 5
>>>len(s1)
15
>>>type(s1)
<class 'str'>
>>>s2='''hello'''
>>>len(s2)
5
>>>type(s2)
<class 'str'>
>>>s
'hello'
>>>s[1]
'e'
>>>s(1)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
s(1)
TypeError: 'str' object is not callable
>>>s[-3]
'l'
>>>s[7]
Traceback (most recent call last):

New Section 1 Page 1


Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
s[7]
IndexError: string index out of range
>>>s
'hello'
>>>type(s)
<class 'str'>
>>>type(s[0])
<class 'str'>
>>>a='1a2b@'
>>>type(a[4])
<class 'str'>
>>>type(s[-5])
<class 'str'>
>>>s
'hello'
>>>s[1]='a'
Traceback (most recent call last):
Item assignment is not possibel.
File "<pyshell#23>", line 1, in <module>
s[1]='a'
TypeError: 'str' object does not support item assignment
>>>str()
'' Default value of string is ' '

New Section 1 Page 2

You might also like