SlideShare una empresa de Scribd logo
1 de 55
PYTHON AND WEB DEVELOPMENT
INSTRUCTIONS TO ATTENDEES
Keep the microphone and camera off throughout the
session
Use hand raise option to acknowledge the host
Post your answers in the chat box
Any specific doubt related to content/problem kindly
ask in the Q&A tab.
A mentor will be allocated to clarify all your doubts
which were posted in Q&A area.
PREVIOUS SESSION
DICTIONARIES AND SETS
PROBLEM SOLVING ON DICTIONARIES AND SETS
CONTENT
STRINGS IN PYTHON
BUILT-IN FUNCTIONS
CODE
• A string is a sequence of characters..
• String literals in python are surrounded by either single quotation marks, or double
quotation marks.
INTRODUCTION
h e l l o
CREATING A STRING
• Strings can be created by enclosing
characters inside a single quote or double-
quotes.
• Anything given as input is basically
considered as string.
ACCESSING ELEMENTS IN A STRING
Like list, string elements can also be accessed using indexing.
0 1 2 3 4 5
P Y T H O N
-6 -5 -4 -3 -2 -1
indexing
Negative
indexing
MODIFY A STRING
• Strings are immutable, hence elements
of a String cannot be changed once it
has been assigned.
• Only new strings can be reassigned to
the same name.
DELETE A STRING
• We can directly delete a string using del
keyword.
• Delete a string using index is not possible.
STRING METHODS AND FUNCTIONS
• String.ascii_letters:
Concatenation of the ascii_lowercase and
ascii_uppercase constants.
• string.ascii_lowercase:
Concatenation of lowercase letters
• string.ascii_uppercase:
Concatenation of uppercase letters
• string.punctuation:
ASCII characters having punctuation characters.
STRING METHODS AND FUNCTIONS
• string.digits:
Digit in strings
• string.hexdigits:
Hexadigit in strings
• string.octdigits:
Octadigit in a string
STRING METHODS AND FUNCTIONS
• string.endswith():
Returns True if a string ends with the given
suffix otherwise returns False
• string.startswith():
Returns True if a string starts with the given
prefix otherwise returns False
• replace():
returns a copy of the string where all
occurrences of a substring is replaced with
another substring.
STRING METHODS AND FUNCTIONS
• string.isdigit():
Returns “True” if all characters in the string
are digits, Otherwise, It returns “False”.
• string.isalpha():
Returns “True” if all characters in the string
are alphabets, Otherwise, It returns “False”.
• string.isdecimal():
Returns true if all characters in a string are
decimal.
STRING METHODS AND FUNCTIONS
• string.isalnum():
Returns true if all the characters in a
given string are alphanumeric.
• string.istitle():
Returns True if the string is a titlecased
string
STRING METHODS AND FUNCTIONS
• string.upper():
Returns the string with all uppercases.
• string.lower():
Returns the string with all lowercases.
• string.swapcase():
Method converts all uppercase
characters to lowercase and vice versa
of the given string, and returns it
STRING METHODS AND FUNCTIONS
• string.partition:
splits the string at the first occurrence of the
separator and returns a tuple.
• string.index:
Returns the position of the first occurrence of
substring in a string.
• string.rindex:
Returns the highest index of the substring inside the
string if substring is found.
• string.splitlines:
Returns a list of lines in the string.
STRING METHODS AND FUNCTIONS
• string.capitalize:
Return a word with its first character
capitalized.
• string.find:
Return the lowest index in a sub string.
• string.rfind:
find the highest index.
• string.count:
Return the number of (non-overlapping)
occurrences of substring sub in string
STRING METHODS AND FUNCTIONS
• len():
Returns the length of the string.
• max():
Returns the highest alphabetical
character in a string.
• min():
Returns the minimum alphabetical
character in a string.
SLICING A STRING
S[:] – prints all the elements from the string.
S[2:] – prints all the elements from the string
starting from index 2
S[:5] – prints all the elements from the string till
the ending index 4
S[2:6] – prints the elements from the string from
index 2 till index 5
S[-4:-2] – prints the elements from the string
from index -4 till index -3
SLICING A STRING
S[::2] – prints all the elements from the string with
step 2 (index+2 element) from the beginning.
S[::-1] – prints all the elements from the string with
step 1 (index+1) from the last.
S[1:6:3] – prints all the elements from the string from
index 1 till index 5 with steps 3.
GUESS THE OUTPUT ?
Guess the correct output of the following String operations
A)WelcomeWelcome
B)TypeError: unsupported operand type(s) for * or pow(): ‘str’ and
‘int’
a
GUESS THE OUTPUT ?
Select the correct output of the following String operations
A)Welcome Coder
B)WelcomCoder
C)Welcom Coder
D)WelcomeCoder
c
GUESS THE OUTPUT ?
What is the output of the following code
A)False
True
B)False
False
C)True
False
a
METHOD
String.ascii_letters:
string.punctuation:
replace():
string.isdecimal():
string.istitle:
string.isalnum:
string.rindex:
string.partition:
string.capitalize:
DESCRIPTION
returns a copy of the string where all occurrences of a substring is
replaced with another substring.
Returns True if the string is a titlecased string
Concatenation of the ascii_lowercase and ascii_uppercase constants.
splits the string at the first occurrence of the separator and returns a
tuple.
ASCII characters having punctuation characters.
Returns true if all characters in a string are decimal.
Returns true if all the characters in a given string are alphanumeric.
Return a word with its first character capitalized.
Returns the highest index of the substring inside the string if
substring is found.
MATCH THE METHOD WITH ITS DESCRIPTION
METHOD
String.ascii_letters:
string.punctuation:
replace():
string.isdecimal():
string.istitle:
string.isalnum:
string.rindex:
string.partition:
string.capitalize:
MATCH THE METHOD WITH ITS DESCRIPTION
DESCRIPTION
returns a copy of the string where all occurrences of a substring is
replaced with another substring.
Returns True if the string is a titlecased string
Concatenation of the ascii_lowercase and ascii_uppercase constants.
splits the string at the first occurrence of the separator and returns a
tuple.
ASCII characters having punctuation characters.
Returns true if all characters in a string are decimal.
Returns true if all the characters in a given string are alphanumeric.
Return a word with its first character capitalized.
Returns the highest index of the substring inside the string if
substring is found.
LET’S START CODING
PROBLEM STATEMENT 1
Write a Python program to calculate the length of a string.
Input Format:
Read a string.
Output Format:
Print the length of a string.
Test cases:
Sample Input Sample Output
coder123 8
programming 11
CODE
PROBLEM STATEMENT 2
Write a Python script that takes input from the user and displays that input back in
upper and lower cases.
Input Format:
Read a string
Output Format:
Print the string in both upper and lower case.
Test Cases:
Sample Input Sample Output
talentio Your input in upper case TALENTIO
Your input in lower case talentio
python Your input in upper case PYTHON
Your input in lower case python
CODE
PROBLEM STATEMENT 3
Write a Python program to swap cases of a given string.
Input Format:
Read a string.
Output Format:
Print the swapped cases of given string.
Test Cases:
Sample Input Sample Output
CoDeR cOdEr
tAlEnTiO TaLeNtIo
CODE
PROBLEM STATEMENT 4
Write a Python program to remove all consecutive duplicates from a given string.
Input Format:
Read a string.
Output Format:
Print the string after removing all consecutive duplicates.
Test Cases:
Sample Input Sample Output
aabcsa "abcsa"
pqqrrsrt "pqrsrt"
CODE
PROBLEM STATEMENT 5
Write a Python program to move all spaces to the front of a given string in single
traversal.
Input Format:
Read a string.
Output Format:
Print the string after eliminating spaces.
Test Cases:
Sample Input Sample Output
Python program " pythonprogram"
try catch exception " trycatchexception"
CODE
PROBLEM STATEMENT 6
Write a Python program to create a string from two given strings concatenating
uncommon characters of the said strings.
Input Format:
Read a string.
Output Format:
Print the new string.
Test Cases:
Sample Input Sample Output
Talentio
Solution
TaSu
Python
Program
ythngam
CODE
PROBLEM STATEMENT 7
Write a Python program to find the maximum occurring character in a given string.
Input Format:
Read a string.
Output Format:
Print the maximum occuring character.
Test Cases:
Sample Input Sample Output
Treat t
Tunnel n
CODE
PROBLEM STATEMENT 8
Write a Python program to check whether a string contains all letters of the alphabet.
Input Format:
Read a string.
Output Format:
Print whether the string contains all alphabets.
Test Cases:
Sample Input Sample Output
The quick brown fox jumps over
the lazy dog
True
Turn over False
CODE
PROBLEM STATEMENT 9
Write a Python program to convert a string in a list.
Input Format:
Read a string.
Output Format:
Print the list after converting the string to list.
Test Cases:
Sample Input Sample Output
"python programmer" ["python" , "programmer"]
Coding challenge ["Coding","challenge"]
CODE
PROBLEM STATEMENT 10
Write a Python program to remove the characters which have odd index values of a
given string.
Input Format:
Read a string.
Output Format:
Print the string after removing odd characters.
Test Cases:
Sample Input Sample Output
abcde "ace"
player "pae"
CODE
YOUR TURN NOW
ASSIGNMENT QUESTION 1
Write a Python function that takes a list of words and returns the length of the
longest one.
Input Format:
Read the list of strings.
Output Format:
Print the length of longest word.
Test Cases:
Sample Input Sample Output
["talentio","coding","programmer
"]
10
["hello","Banglore"] 8
ASSIGNMENT QUESTION 2
Write a Python program to capitalize first and last letters of each word of a given
string.
Input Format:
Read the string.
Output Format:
Print the string after captalizing first and last letters in each word.
Test Cases:
Sample Input Sample Output
programmer ProgrammeR
coding CodinG
SUMMARY
STRINGS AND ITS BUILTIN FUNCTIONS AND METHODS
PROBLEM SOLVING ON STRINGS
PRACTICE CODING
ANY QUERIES?
POST TRAINING - DOUBTS
OR
Scan this QR code and
say “Hi”
KEEP WAITING FOR NEXT SESSION
THANK YOU

Más contenido relacionado

Similar a STRINGS_IN_PYTHON 9-12 (1).pptx

Similar a STRINGS_IN_PYTHON 9-12 (1).pptx (20)

Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Python Basics
Python BasicsPython Basics
Python Basics
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Lenguaje Python
Lenguaje PythonLenguaje Python
Lenguaje Python
 
pysdasdasdsadsadsadsadsadsadasdasdthon1.ppt
pysdasdasdsadsadsadsadsadsadasdasdthon1.pptpysdasdasdsadsadsadsadsadsadasdasdthon1.ppt
pysdasdasdsadsadsadsadsadsadasdasdthon1.ppt
 
coolstuff.ppt
coolstuff.pptcoolstuff.ppt
coolstuff.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Introductio_to_python_progamming_ppt.ppt
Introductio_to_python_progamming_ppt.pptIntroductio_to_python_progamming_ppt.ppt
Introductio_to_python_progamming_ppt.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
gdscpython.pdf
gdscpython.pdfgdscpython.pdf
gdscpython.pdf
 
Python ppt
Python pptPython ppt
Python ppt
 
manish python.pptx
manish python.pptxmanish python.pptx
manish python.pptx
 
STRINGS IN PYTHON
STRINGS IN PYTHONSTRINGS IN PYTHON
STRINGS IN PYTHON
 

Último

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Último (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

STRINGS_IN_PYTHON 9-12 (1).pptx

  • 1. PYTHON AND WEB DEVELOPMENT
  • 2. INSTRUCTIONS TO ATTENDEES Keep the microphone and camera off throughout the session Use hand raise option to acknowledge the host Post your answers in the chat box Any specific doubt related to content/problem kindly ask in the Q&A tab. A mentor will be allocated to clarify all your doubts which were posted in Q&A area.
  • 3. PREVIOUS SESSION DICTIONARIES AND SETS PROBLEM SOLVING ON DICTIONARIES AND SETS
  • 5. • A string is a sequence of characters.. • String literals in python are surrounded by either single quotation marks, or double quotation marks. INTRODUCTION h e l l o
  • 6. CREATING A STRING • Strings can be created by enclosing characters inside a single quote or double- quotes. • Anything given as input is basically considered as string.
  • 7. ACCESSING ELEMENTS IN A STRING Like list, string elements can also be accessed using indexing. 0 1 2 3 4 5 P Y T H O N -6 -5 -4 -3 -2 -1 indexing Negative indexing
  • 8. MODIFY A STRING • Strings are immutable, hence elements of a String cannot be changed once it has been assigned. • Only new strings can be reassigned to the same name.
  • 9. DELETE A STRING • We can directly delete a string using del keyword. • Delete a string using index is not possible.
  • 10. STRING METHODS AND FUNCTIONS • String.ascii_letters: Concatenation of the ascii_lowercase and ascii_uppercase constants. • string.ascii_lowercase: Concatenation of lowercase letters • string.ascii_uppercase: Concatenation of uppercase letters • string.punctuation: ASCII characters having punctuation characters.
  • 11. STRING METHODS AND FUNCTIONS • string.digits: Digit in strings • string.hexdigits: Hexadigit in strings • string.octdigits: Octadigit in a string
  • 12. STRING METHODS AND FUNCTIONS • string.endswith(): Returns True if a string ends with the given suffix otherwise returns False • string.startswith(): Returns True if a string starts with the given prefix otherwise returns False • replace(): returns a copy of the string where all occurrences of a substring is replaced with another substring.
  • 13. STRING METHODS AND FUNCTIONS • string.isdigit(): Returns “True” if all characters in the string are digits, Otherwise, It returns “False”. • string.isalpha(): Returns “True” if all characters in the string are alphabets, Otherwise, It returns “False”. • string.isdecimal(): Returns true if all characters in a string are decimal.
  • 14. STRING METHODS AND FUNCTIONS • string.isalnum(): Returns true if all the characters in a given string are alphanumeric. • string.istitle(): Returns True if the string is a titlecased string
  • 15. STRING METHODS AND FUNCTIONS • string.upper(): Returns the string with all uppercases. • string.lower(): Returns the string with all lowercases. • string.swapcase(): Method converts all uppercase characters to lowercase and vice versa of the given string, and returns it
  • 16. STRING METHODS AND FUNCTIONS • string.partition: splits the string at the first occurrence of the separator and returns a tuple. • string.index: Returns the position of the first occurrence of substring in a string. • string.rindex: Returns the highest index of the substring inside the string if substring is found. • string.splitlines: Returns a list of lines in the string.
  • 17. STRING METHODS AND FUNCTIONS • string.capitalize: Return a word with its first character capitalized. • string.find: Return the lowest index in a sub string. • string.rfind: find the highest index. • string.count: Return the number of (non-overlapping) occurrences of substring sub in string
  • 18. STRING METHODS AND FUNCTIONS • len(): Returns the length of the string. • max(): Returns the highest alphabetical character in a string. • min(): Returns the minimum alphabetical character in a string.
  • 19. SLICING A STRING S[:] – prints all the elements from the string. S[2:] – prints all the elements from the string starting from index 2 S[:5] – prints all the elements from the string till the ending index 4 S[2:6] – prints the elements from the string from index 2 till index 5 S[-4:-2] – prints the elements from the string from index -4 till index -3
  • 20. SLICING A STRING S[::2] – prints all the elements from the string with step 2 (index+2 element) from the beginning. S[::-1] – prints all the elements from the string with step 1 (index+1) from the last. S[1:6:3] – prints all the elements from the string from index 1 till index 5 with steps 3.
  • 21. GUESS THE OUTPUT ? Guess the correct output of the following String operations A)WelcomeWelcome B)TypeError: unsupported operand type(s) for * or pow(): ‘str’ and ‘int’ a
  • 22. GUESS THE OUTPUT ? Select the correct output of the following String operations A)Welcome Coder B)WelcomCoder C)Welcom Coder D)WelcomeCoder c
  • 23. GUESS THE OUTPUT ? What is the output of the following code A)False True B)False False C)True False a
  • 24. METHOD String.ascii_letters: string.punctuation: replace(): string.isdecimal(): string.istitle: string.isalnum: string.rindex: string.partition: string.capitalize: DESCRIPTION returns a copy of the string where all occurrences of a substring is replaced with another substring. Returns True if the string is a titlecased string Concatenation of the ascii_lowercase and ascii_uppercase constants. splits the string at the first occurrence of the separator and returns a tuple. ASCII characters having punctuation characters. Returns true if all characters in a string are decimal. Returns true if all the characters in a given string are alphanumeric. Return a word with its first character capitalized. Returns the highest index of the substring inside the string if substring is found. MATCH THE METHOD WITH ITS DESCRIPTION
  • 25. METHOD String.ascii_letters: string.punctuation: replace(): string.isdecimal(): string.istitle: string.isalnum: string.rindex: string.partition: string.capitalize: MATCH THE METHOD WITH ITS DESCRIPTION DESCRIPTION returns a copy of the string where all occurrences of a substring is replaced with another substring. Returns True if the string is a titlecased string Concatenation of the ascii_lowercase and ascii_uppercase constants. splits the string at the first occurrence of the separator and returns a tuple. ASCII characters having punctuation characters. Returns true if all characters in a string are decimal. Returns true if all the characters in a given string are alphanumeric. Return a word with its first character capitalized. Returns the highest index of the substring inside the string if substring is found.
  • 27. PROBLEM STATEMENT 1 Write a Python program to calculate the length of a string. Input Format: Read a string. Output Format: Print the length of a string. Test cases: Sample Input Sample Output coder123 8 programming 11
  • 28. CODE
  • 29. PROBLEM STATEMENT 2 Write a Python script that takes input from the user and displays that input back in upper and lower cases. Input Format: Read a string Output Format: Print the string in both upper and lower case. Test Cases: Sample Input Sample Output talentio Your input in upper case TALENTIO Your input in lower case talentio python Your input in upper case PYTHON Your input in lower case python
  • 30. CODE
  • 31. PROBLEM STATEMENT 3 Write a Python program to swap cases of a given string. Input Format: Read a string. Output Format: Print the swapped cases of given string. Test Cases: Sample Input Sample Output CoDeR cOdEr tAlEnTiO TaLeNtIo
  • 32. CODE
  • 33. PROBLEM STATEMENT 4 Write a Python program to remove all consecutive duplicates from a given string. Input Format: Read a string. Output Format: Print the string after removing all consecutive duplicates. Test Cases: Sample Input Sample Output aabcsa "abcsa" pqqrrsrt "pqrsrt"
  • 34. CODE
  • 35. PROBLEM STATEMENT 5 Write a Python program to move all spaces to the front of a given string in single traversal. Input Format: Read a string. Output Format: Print the string after eliminating spaces. Test Cases: Sample Input Sample Output Python program " pythonprogram" try catch exception " trycatchexception"
  • 36. CODE
  • 37. PROBLEM STATEMENT 6 Write a Python program to create a string from two given strings concatenating uncommon characters of the said strings. Input Format: Read a string. Output Format: Print the new string. Test Cases: Sample Input Sample Output Talentio Solution TaSu Python Program ythngam
  • 38. CODE
  • 39. PROBLEM STATEMENT 7 Write a Python program to find the maximum occurring character in a given string. Input Format: Read a string. Output Format: Print the maximum occuring character. Test Cases: Sample Input Sample Output Treat t Tunnel n
  • 40. CODE
  • 41. PROBLEM STATEMENT 8 Write a Python program to check whether a string contains all letters of the alphabet. Input Format: Read a string. Output Format: Print whether the string contains all alphabets. Test Cases: Sample Input Sample Output The quick brown fox jumps over the lazy dog True Turn over False
  • 42. CODE
  • 43. PROBLEM STATEMENT 9 Write a Python program to convert a string in a list. Input Format: Read a string. Output Format: Print the list after converting the string to list. Test Cases: Sample Input Sample Output "python programmer" ["python" , "programmer"] Coding challenge ["Coding","challenge"]
  • 44. CODE
  • 45. PROBLEM STATEMENT 10 Write a Python program to remove the characters which have odd index values of a given string. Input Format: Read a string. Output Format: Print the string after removing odd characters. Test Cases: Sample Input Sample Output abcde "ace" player "pae"
  • 46. CODE
  • 48. ASSIGNMENT QUESTION 1 Write a Python function that takes a list of words and returns the length of the longest one. Input Format: Read the list of strings. Output Format: Print the length of longest word. Test Cases: Sample Input Sample Output ["talentio","coding","programmer "] 10 ["hello","Banglore"] 8
  • 49. ASSIGNMENT QUESTION 2 Write a Python program to capitalize first and last letters of each word of a given string. Input Format: Read the string. Output Format: Print the string after captalizing first and last letters in each word. Test Cases: Sample Input Sample Output programmer ProgrammeR coding CodinG
  • 50. SUMMARY STRINGS AND ITS BUILTIN FUNCTIONS AND METHODS PROBLEM SOLVING ON STRINGS
  • 53. POST TRAINING - DOUBTS OR Scan this QR code and say “Hi”
  • 54. KEEP WAITING FOR NEXT SESSION