Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views25 pages

Week 6 String

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)
21 views25 pages

Week 6 String

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/ 25

OCS1903

PROGRAMMING USING PYTHON

String in Python

WEEK 06
CONTENTS
01 02 03
String-Intro String Creation String Indexing

04 05 06
String Operators String Methods and Problem Solving
Functions
String-Introduction
Eg :”Vijaya Kumar“
• String is defined as a sequence of characters. ‘rec’
• String can contain alphabets, numbers and special characters.
• String is either enclosed within “ “(double quotes) or ‘ ‘(single
quotes)
Strings are
• Strings are immutable in nature-characters of a string cannot be immutable

modified.
• Characters of a String can be accessed by their index.
String Creation

• Strings can be created directly by


assigning a variable with a string
value.
• String can also be created using
the str() function.
• This function returns the string
version of the passed object.
String Indexing

• String indexing is zero-based:


the first character in the string
has index 0, the next is 1, and
so on.
• Negative string indexing is
also allowed in python.
• Here the last character will be
indexed as -1,the last next will
be -2 and s on.
Strings are Immutable
• Since Strings are immutable , characters cannot be modified

Example OUTPUT
String Operators

Slicing
Membership and
Concatenation
identity

Repetition
‘s’ is different from ‘S’????

ASCII VALUE

Each and every character in a string is associated


with a decimal value(alphabets, integer and special
characters)
String :Built-in Methods

Case conversion methods Find and Replace Methods

Character classification
String Formatting Methods
methods
Case conversion methods
Methods in this group perform case conversion on the target string.
s.capitalize() returns a copy of s with the first character converted to uppercase
and all other characters converted to lowercase

s.lower() returns a copy of s with all alphabetic characters converted to


lowercase

s.swapcase() returns a copy of s with uppercase alphabetic characters converted


to lowercase and vice versa

s.upper() returns a copy of s with all alphabetic characters converted to


uppercase
Case conversion methods

Methods in this group perform case conversion on the target string.

Original string is not


modified new strings
are created
Find methods
These methods provide various means of searching the target string for a
specified substring.

count(substring ,start , end):Counts occurrences of a substring in the target string


endswith (substring ,start , end) :Determines whether the target string ends with a given
substring.
find (substring ,start , end): Searches the target string for a given substring.
rfind (substring ,start , end) :Searches the target string for a given substring starting at the end.
startswith(substring ,start , end): Determines whether the target string starts with a given
substring.
Find methods

These methods provide various means of searching the target string for a
specified substring.
Replace method

s.replace(old , new , count): Replaces occurrences of a substring


within a string and returns a new string.
Character classification methods

Methods in this group classify a string based on the characters it contains.

isalnum(): Determines whether the target string consists of alphanumeric characters.


isalpha():Determines whether the target string consists of alphabetic characters.
isdigit():Determines whether the target string consists of digit characters.
islower():Determines whether the target string’s alphabetic characters are lowercase.
isspace():Determines whether the target string consists of whitespace characters.
isupper():Determines whether the target string’s alphabetic characters are uppercase.
istitle():Determines whether the target string is title cased.
center():Method will center align the string, using a specified character (space is default) as
the fill character.
Character classification methods

Methods in this group classify a string based on the characters it contains.


String formatting methods

Methods in this group modify or enhance the format of a string.

Refer the below url,


https://realpython.com/python-strings/#specifying-a-stride-in-a-string-slice
Conversion between string and list

Methods in this group convert between a string and some composite data type by
either pasting objects together to make a string, or by breaking a string up into
pieces.
join(<iterable>) split(delimeter)
Concatenates strings from an iterable. Splits a string into a list of substrings.
Build in functions
Example 1

Write a Python program that takes a complete sentence as an input and find whether
each word is a palindrome or not.
Input:
“My mom and dad speak
malayalam”
Output:
My: not palindrome
Mom: palindrome
and: not palindrome
speak: not palindrome
malayalam : palindrome
Example 2

There is a robot starting at theRobot


positionReturn to Origin
(0, 0), the origin, on a 2D plane. Given a sequence
of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

You are given a string moves that represents the move sequence of the robot where
moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D'
(down).

Return true if the robot returns to the origin after it finishes all of its moves, or false
otherwise.

Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot
move to the right once, 'L' will always make it move left, etc. Also, assume that the
magnitude of the robot's movement is the same for each move.
Example 2(COND)

Example-1: Example-2:
Input: moves = "UD" Input: moves = “LL"
Output: true Output: false
Explanation: The robot moves up once, and Explanation: The robot moves left twice. It
then down once. All moves have the same ends up two "moves" to the left of the
magnitude, so it ended up at the origin origin. We return false because it is not at
where it started. Therefore, we return true. the origin at the end of its moves.
Example 3
Rotate String
• Given two strings s and goal, PRINT true if and only if s can become goal after
some number of shifts on s.
• A shift on s consists of moving the leftmost character of s to the rightmost position.
• For example, if s = "abcde", then it will be "bcdea" after one shift.
• INPUT: s = "abcde", goal = "cdeab“
• OUTPUT: true
• INPUT: s = "abcde", goal = "abced"
• OUTPUT: false
Example 4

• Given two strings s and t, Print true if t is an anagram of s, and false otherwise.
• An Anagram is a word or phrase formed by rearranging the letters of a different
word or phrase, typically using all the original letters exactly

• INPUT: s = "anagram", t = "nagaram“


• OUTPUT: true
• INPUT: s = "rat", t = "car“
• OUTPUT: false
Department of Computer Science and Engi 25
neering

You might also like