Grade 11 Exam  >  Grade 11 Notes  >  Computer Science  >  String Manipulation in Python

String Manipulation in Python

Introduction

Python strings are characters enclosed in quotes of any type - single quotation marks, double quotation marks, triple quotation marks.

  • S = 'Anjeev Singh Academy' ,    
  • T =  "Singh@123"
  • Mline = '''Anjeev Singh Academy

is the best free online academy on YouTube '''
Python strings is an immutable data type, i.e. not allowed to change in place. S[0] ='R' not allowed

String Indexing - Forward and Backward Indexing in Python

Strings are sequence of characters, where each character has a unique index number.
string = "mycstutorial.in"
>>> string[0]
'm'
>>> string[-1]
'n'

Two types of Indexing is supported in python.

  • Forward Indexing and
  • Backward Indexing

The indexes of strings begin from 0 to length-1, in forward direction and  -1, -2, -3, .... , -length in backward direction.

String Indexing - Forward and Backward Indexing in Python

Strings can be manipulated by using string operators. String operators are

Strings can be manipulated by using string operators. String operators are

Traversing a string using loops

Traversing a String or Text means accessing all characters of string, character by character, through its index position.
#Traversal of a string using loop
name = "mycstutorial.in"
for char in name:
print (char, end=' ')

Output
m y c s t u t o r i a l . i n

Traversing a String using index position

#Traversal of a string using index - forward indexing
name = "mycstutorial.in"
for index in range(len(name)):
print (name[index], end=' ')

Output
m y c s t u t o r i a l . i n

#Traversal of a string using index - backward indexing
name = "mycstutorial.in"
for index in range(-1, -len(name)-1, -1):
print (name[index], end=' ')

Output
n i . l a i r o t u t s c y m 

String Slicing

Slice means 'a part of'.  In Python, string slice refers to a part of string containing some contiguous characters from the string.
Syntax: string[start : end : step]

  • where start and end are integers with legal indices.
  • will return the characters falling between indices start and end. i.e. start, start+1, start+2,..., till end-1.
  • By default value of step is 1. It is an optional argument.

String Slicing

String Built-in functions

  • Python provides several built-in-methods to manipulate string. 
  • Syntax : stringvariable.methodName( )
  • Example: isalnum(), isalpha(), isdigit(), islower(), isupper(), isspace(), etc

String SlicingString SlicingString Slicing

The document String Manipulation in Python is a part of the Grade 11 Course Computer Science for Grade 11.
All you need of Grade 11 at this link: Grade 11
Explore Courses for Grade 11 exam
Get EduRev Notes directly in your Google search
Related Searches
mock tests for examination, Summary, video lectures, Free, Sample Paper, shortcuts and tricks, Important questions, practice quizzes, past year papers, study material, String Manipulation in Python, MCQs, pdf , Semester Notes, Extra Questions, Exam, String Manipulation in Python, Previous Year Questions with Solutions, Viva Questions, String Manipulation in Python, Objective type Questions, ppt;