SlideShare a Scribd company logo
Python- Strings
• Strings are amongst the most popular types in Python.
• We can create them simply by enclosing characters in quotes.
• Python treats single quotes the same as double quotes.
• Creating strings is as simple as assigning a value to a variable.
For example
var1 = 'Hello World!'
var2 = "Python Programming"
Accessing Values in Strings
• Python does not support a character type; these are treated as strings of length one, thus
also considered a substring.
• To access substrings, use the square brackets for slicing along with the index or indices to
obtain your substring.
1
Python- Strings
• Example:
var1 = 'Hello World!'
var2 = "Python Programming"
print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])
• Updating Strings
You can "update" an existing string by (re)assigning a variable to another string.
The new value can be related to its previous value or to a completely different string
altogether.
For example-
#!/usr/bin/python3
var1 = 'Hello World!'
print ("Updated String :- ", var1[:6] + 'Python')
Output:
var1[0]: H
var2[1:5]: ytho
Output:
Updated String :- Hello Python
2
Python- Strings
• Escape Characters
• Following table is a list of escape or non-printable characters that can be represented with
backslash notation.
• An escape character gets interpreted; in a single quoted as well as double quoted strings.
3
Python- Strings
• String Special Operators
• Assume string variable a holds 'Hello' and variable b holds 'Python', then-
4
Python- Strings
• String Special Operators
5
Python- Strings
• String Formatting Operators
6
Python- Strings
• String Formatting Operators
7
Python- Strings
• Other supported symbols and functionality
8
Python- Strings
• Unicode String
• In Python 3, all strings are represented in Unicode. In Python 2 are stored internally as 8-
bit ASCII, hence it is required to attach 'u' to make it Unicode. It is no longer necessary
now.
• Built-in String Methods
• Python includes the following built-in methods to manipulate strings-
9
Python- Strings
• Unicode String
10
Python- Strings
• Unicode String
11
Python- Strings
• Unicode String
12
Python- Strings
• Unicode String
13
Python- Strings
• Unicode String
14
Python- Strings
• Unicode String
15
Python- Strings
• Unicode String
16
Python- Strings
• Unicode String
String capitalize() Method
It returns a copy of the string with only its first character capitalized.
Syntax
str.capitalize()
Example:
str = "this is string example”
print ("str.capitalize() : ", str.capitalize())
Output:
str.capitalize() : This is string example
17
Python- Strings
Output:
str.capitalize() : This is string example
str.center(40, 'a') : aaaathis is string
exampleaaaa
String center() Method
The method center() returns centered in a string of length width. Padding is done using
the specified fillchar. Default filler is a space.
Syntax
str.center(width[, fillchar])
Parameters
• width - This is the total width of the string.
• fillchar - This is the filler character.
Return Value
This method returns a string that is at least width characters wide, created by padding the
string with the character fillchar (default is a space).
Example
The following example shows the usage of the center() method.
#!/usr/bin/python3
str = "this is string example.
print ("str.center(40, 'a') : ", str.center(40, 'a'))
18

More Related Content

Similar to Unit 1-Part-3 - String Manipulation.pdf (20)

PPTX
Fundamentals of Python Programming
Kamal Acharya
 
PPSX
Programming with Python
Rasan Samarasinghe
 
PPTX
Python unit 2 is added. Has python related programming content
swarna16
 
PDF
Strings in python
Prabhakaran V M
 
PDF
Python interview questions
Pragati Singh
 
PPTX
Introduction to Python Programming .pptx
NaynaSagarDahatonde
 
PPTX
Python-Basics.pptx
TamalSengupta8
 
PPTX
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
PPTX
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
PDF
stringsinpython-181122100212.pdf
paijitk
 
PPTX
Python ppt
Anush verma
 
PPT
Python Basics
MobeenAhmed25
 
PPT
python1.ppt
ALOK52916
 
PPT
python1.ppt
RajPurohit33
 
PPT
Lenguaje Python
RalAnteloJurado
 
PPT
python1.ppt
RedenOriola
 
PPT
python1.ppt
AshokRachapalli1
 
PPT
python1.ppt
JemuelPinongcos1
 
PPT
Learn Python in Three Hours - Presentation
Naseer-ul-Hassan Rehman
 
PPT
python1.ppt
VishwasKumar58
 
Fundamentals of Python Programming
Kamal Acharya
 
Programming with Python
Rasan Samarasinghe
 
Python unit 2 is added. Has python related programming content
swarna16
 
Strings in python
Prabhakaran V M
 
Python interview questions
Pragati Singh
 
Introduction to Python Programming .pptx
NaynaSagarDahatonde
 
Python-Basics.pptx
TamalSengupta8
 
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
stringsinpython-181122100212.pdf
paijitk
 
Python ppt
Anush verma
 
Python Basics
MobeenAhmed25
 
python1.ppt
ALOK52916
 
python1.ppt
RajPurohit33
 
Lenguaje Python
RalAnteloJurado
 
python1.ppt
RedenOriola
 
python1.ppt
AshokRachapalli1
 
python1.ppt
JemuelPinongcos1
 
Learn Python in Three Hours - Presentation
Naseer-ul-Hassan Rehman
 
python1.ppt
VishwasKumar58
 

More from Harsha Patil (20)

PDF
Unit 5-Introduction of GUI Programming-Part2.pdf
Harsha Patil
 
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
PDF
Unit 6-Introduction of Python Libraries.pdf
Harsha Patil
 
PDF
Unit 5-Introduction of GUI Programming-Part1.pdf
Harsha Patil
 
PDF
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
PDF
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
PDF
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
PDF
Unit 1-Part-1-Introduction to Python.pdf
Harsha Patil
 
PDF
Unit 1-Part-5-Functions and Set Operations.pdf
Harsha Patil
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPTX
Introduction to Reinforcement Learning.pptx
Harsha Patil
 
PPTX
Introduction to Association Rules.pptx
Harsha Patil
 
PPTX
Introduction to Clustering . pptx
Harsha Patil
 
PPTX
Introduction to Classification . pptx
Harsha Patil
 
PPTX
Introduction to Regression . pptx
Harsha Patil
 
PPTX
Intro of Machine Learning Models .pptx
Harsha Patil
 
PPTX
Introduction to Machine Learning.pptx
Harsha Patil
 
PPTX
Unit-V-Introduction to Data Mining.pptx
Harsha Patil
 
PPTX
Unit-IV-Introduction to Data Warehousing .pptx
Harsha Patil
 
PPTX
Unit-III-AI Search Techniques and solution's
Harsha Patil
 
Unit 5-Introduction of GUI Programming-Part2.pdf
Harsha Patil
 
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
Unit 6-Introduction of Python Libraries.pdf
Harsha Patil
 
Unit 5-Introduction of GUI Programming-Part1.pdf
Harsha Patil
 
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
Unit 1-Part-1-Introduction to Python.pdf
Harsha Patil
 
Unit 1-Part-5-Functions and Set Operations.pdf
Harsha Patil
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
Introduction to Reinforcement Learning.pptx
Harsha Patil
 
Introduction to Association Rules.pptx
Harsha Patil
 
Introduction to Clustering . pptx
Harsha Patil
 
Introduction to Classification . pptx
Harsha Patil
 
Introduction to Regression . pptx
Harsha Patil
 
Intro of Machine Learning Models .pptx
Harsha Patil
 
Introduction to Machine Learning.pptx
Harsha Patil
 
Unit-V-Introduction to Data Mining.pptx
Harsha Patil
 
Unit-IV-Introduction to Data Warehousing .pptx
Harsha Patil
 
Unit-III-AI Search Techniques and solution's
Harsha Patil
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
community health nursing question paper 2.pdf
Prince kumar
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Ad

Unit 1-Part-3 - String Manipulation.pdf

  • 1. Python- Strings • Strings are amongst the most popular types in Python. • We can create them simply by enclosing characters in quotes. • Python treats single quotes the same as double quotes. • Creating strings is as simple as assigning a value to a variable. For example var1 = 'Hello World!' var2 = "Python Programming" Accessing Values in Strings • Python does not support a character type; these are treated as strings of length one, thus also considered a substring. • To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. 1
  • 2. Python- Strings • Example: var1 = 'Hello World!' var2 = "Python Programming" print ("var1[0]: ", var1[0]) print ("var2[1:5]: ", var2[1:5]) • Updating Strings You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. For example- #!/usr/bin/python3 var1 = 'Hello World!' print ("Updated String :- ", var1[:6] + 'Python') Output: var1[0]: H var2[1:5]: ytho Output: Updated String :- Hello Python 2
  • 3. Python- Strings • Escape Characters • Following table is a list of escape or non-printable characters that can be represented with backslash notation. • An escape character gets interpreted; in a single quoted as well as double quoted strings. 3
  • 4. Python- Strings • String Special Operators • Assume string variable a holds 'Hello' and variable b holds 'Python', then- 4
  • 5. Python- Strings • String Special Operators 5
  • 6. Python- Strings • String Formatting Operators 6
  • 7. Python- Strings • String Formatting Operators 7
  • 8. Python- Strings • Other supported symbols and functionality 8
  • 9. Python- Strings • Unicode String • In Python 3, all strings are represented in Unicode. In Python 2 are stored internally as 8- bit ASCII, hence it is required to attach 'u' to make it Unicode. It is no longer necessary now. • Built-in String Methods • Python includes the following built-in methods to manipulate strings- 9
  • 17. Python- Strings • Unicode String String capitalize() Method It returns a copy of the string with only its first character capitalized. Syntax str.capitalize() Example: str = "this is string example” print ("str.capitalize() : ", str.capitalize()) Output: str.capitalize() : This is string example 17
  • 18. Python- Strings Output: str.capitalize() : This is string example str.center(40, 'a') : aaaathis is string exampleaaaa String center() Method The method center() returns centered in a string of length width. Padding is done using the specified fillchar. Default filler is a space. Syntax str.center(width[, fillchar]) Parameters • width - This is the total width of the string. • fillchar - This is the filler character. Return Value This method returns a string that is at least width characters wide, created by padding the string with the character fillchar (default is a space). Example The following example shows the usage of the center() method. #!/usr/bin/python3 str = "this is string example. print ("str.center(40, 'a') : ", str.center(40, 'a')) 18