Syntax, Example - Strings - Python | Problem Solving and Python Programming : Control Flow, Functions

Chapter: Problem Solving and Python Programming : Control Flow, Functions

Strings - Python

Strings - String slices - Immutability - String functions and methods - String module

Strings:

 

v       Strings

v       String slices

v       Immutability

v       String functions and methods

v       String module

 

Strings:

v       String is defined as sequence of characters represented in quotation marks (either single quotes ( ‘ ) or double quotes ( “ ).

v       An individual character in a string is accessed using a index.

v       The index should always be an integer (positive or negative).

v       A index starts from 0 to n-1.

v       Strings are immutable i.e. the contents of the string cannot be changed after it is created.

v       Python will get the input at run time by default as a string.

v       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:

v                 A part of a string is called string slices.

v                 The process of extracting a sub string from a string is called slicing.


 

Immutability:

v                 Python strings are “immutable” as they cannot be changed after they are created.

v                 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:

v       A module is a file containing Python definitions, functions, statements.

v       Standard library of Python is extended as modules.

v       To use these modules in a program, programmer needs to import the module.

v       Once we import a module, we can reference or use to any of its functions or variables in our code.

v       There is large number of standard modules also available in python.

v       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("happ

y birthday"))

print(string.hexdigits)

print(string.octdigits)

 

output

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

0123456789

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJ

KLMNOPQRSTUVWXYZ!"#$%&'()*+,-

./:;<=>?@[\]^_`{|}~

Happy Birthday

0123456789abcdefABCDEF

01234567

 

 

Escape sequences in string


 

Tags : Syntax, Example , Problem Solving and Python Programming : Control Flow, Functions
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Control Flow, Functions : Strings - Python | Syntax, Example


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.