0% found this document useful (0 votes)
13 views15 pages

Strings

Uploaded by

durgadevi
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 views15 pages

Strings

Uploaded by

durgadevi
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/ 15

STRINGS

Strings
 Strings
 String slices
 Immutability
 String functions and methods
 String module
STRINGS
 String is defined as sequence of characters represented in quotation marks (either single quotes ( ‘ )
or double quotes ( “ ).
 An individual character in a string is accessed using a index.
 The index should always be an integer (positive or negative).
 A index starts from 0 to n-1.
 Strings are immutable i.e. the contents of the string cannot be changed after it is created.
 Python will get the input at run time by default as a string.
 Python does not support character data type. A string of size 1 can be treated as characters.
1. single quotes (' ')
2. double quotes (" ")
3. triple quotes(“”” “”””)
Operations on string:
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
5. Member ship
String slices:
 A part of a string is called string slices.
 The process of extracting a sub string from a string is called slicing.
Immutability:
 Python strings are “immutable” as they cannot be changed after
they are created.
 Therefore [ ] operator cannot be used on the left side of an
assignment.
String built in functions and methods:
 A method is a function that “belongs to” an object.
Syntax to access the method
Stringname.method()
a=”happy birthday”
here, a is the string name.
String modules:
 A module is a file containing Python definitions, functions, statements.
 Standard library of Python is extended as modules.
 To use these modules in a program, programmer needs to import the module.
 Once we import a module, we can reference or use to any of its functions or variables in
our code.
 There is large number of standard modules also available in python.
 Standard modules can be imported the same way as we import our user-defined
modules.
Syntax:
import module_name
Example
import string
print(string.punctuation)
print(string.digits)
print(string.printable)
print(string.capwords("happy birthday"))
print(string.hexdigits)
print(string.octdigits)
output
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
0123456789
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJ
KLMNOPQRSTUVWXYZ!"#$%&'()*+,-
./:;<=>?@[\]^_`{|}~
Happy Birthday
0123456789abcdefABCDEF
01234567
Escape sequences in string

You might also like