LECTURE – STRINGS
BY: ABHISHEK BHARDWAJ
PGT- COMPUTER SCIENCE
Strings in Python
Strings in Python
String Printing
String Operators
String Operators
Character ASCII Code
A 65
B 66
a 97
b 98
String Slicing
String Slicing
D
String Functions
Length of the String & Printing String Character by
Character (Traversing a String using Loop)
CODE SNIPPET
OUTPUT
CODE SNIPPET
OUTPUT
Capitalize String’s First Character
CODE SNIPPET
OUTPUT
OUTPUT
From the above Output, it is clear that capitalize() only
changes the String shown in output, but it does not
change the Original String.
Program to Find Reverse a String
(Method 1 : String Slicing)
CODE SNIPPET
OUTPUT
Start from
Backward Index
i. e. -1 Index
End not defined means
default till end i. e. 0 Index
Step Value
i.e. -1
Decrease by 1 in each step
Program to Find Reverse a String
(Method 2 : Programming Capabilities)
CODE SNIPPET
OUTPUT
Length of String
-1 means last
character of
string
End is defined as -1 because it
is taken as one less than
actual value i.e. 0
Step Value
i.e. -1
Program to Count Total Vowels and Consonants in a
String
CODE SNIPPET
Start from 0
Index
Step Value by default will
increase by 1 in each step
OUTPUT
End is defined as end of
string index
String Functions
Program to Find Lowest Index in the String where
Substring is found
CODE SNIPPET
Start from 0
Index
End Index one less from
the string length
OUTPUT
Substring to Find
Program to Check whether given String is Palindrome
or Not?
CODE SNIPPET
Step Value
i.e. -1
Decrease by 1 in each step
OUTPUT
Start from
Backward Index
i. e. -1 Index
End not defined means
default till end i. e. 0 Index
String Functions
String Functions
String Functions
String Functions
title() : Make the first letter in each word upper
case.
CODE SNIPPET OUTPUT
lower() : Lower case the string
CODE SNIPPET OUTPUT
String Functions
upper() : Upper case the string.
CODE SNIPPET OUTPUT
count() : Return the number of times a specified
value appears in the string.
CODE SNIPPET OUTPUT
String Functions
index() : The index() method finds the first occurrence of
the specified value. The index() method raises an
exception if the value is not found .
CODE SNIPPET OUTPUT
String Functions
endswith() : The endswith() method returns True if the
string ends with the specified value, otherwise False.
Syntax : string.endswith(value, start, end).
CODE SNIPPET OUTPUT
String Functions
startswith() : The startswith() method returns True if the
string starts with the specified value, otherwise False.
Syntax : string.startswith(value, start, end)
CODE SNIPPET OUTPUT
String Functions
isalpha() : The isalpha() method returns True if all the
characters are alphabet letters (a-z).
Syntax : string.isalpha()
CODE SNIPPET OUTPUT
String Functions
islower() : The islower() method returns True if all the
characters in the text are in lower case, otherwise
False. Numbers, symbols and spaces are not checked.
Syntax : string.islower()
CODE SNIPPET OUTPUT
String Functions
isupper() : The isupper() method returns True if all the
characters in the text are in upper case, otherwise
False. Numbers, symbols and spaces are not checked.
Syntax : string.isupper()
CODE SNIPPET OUTPUT
String Functions
lstrip() : The lstrip() method removes any leading
characters. (space is the default leading character to
remove)
Syntax : string.lstrip(characters)
CODE SNIPPET OUTPUT
String Functions
rstrip() : The rstrip() method removes any trailing
characters (characters at the end a string), space is the
default trailing character to remove.
Syntax : string.rstrip(characters)
CODE SNIPPET OUTPUT
String Functions
strip() : The strip() method removes any leading (spaces at
the beginning) and trailing (spaces at the end) characters.
(space is the default leading character to remove)
Syntax : string.strip(characters)
CODE SNIPPET OUTPUT
String Functions
replace() : The replace() method replaces a specified
phrase with another specified phrase.
Syntax : string.replace(oldvalue, newvalue, count)
CODE SNIPPET
OUTPUT
String Functions
join() : The join() method takes all items in an iterable and
joins them into one string. A string must be specified as
the separator.
Syntax : string.join(iterable)  Any iterable object where
all the returned values are strings
CODE SNIPPET OUTPUT
CODE SNIPPET OUTPUT
When using a dictionary as an iterable, the returned values are the keys, not the values.
String Functions
partition() : The partition() method searches for a specified string,
and splits the string into a tuple containing three elements.
 The first element contains the part before the specified string.
 The second element contains the specified string.
 The third element contains the part after the string.
Syntax : string.partition(value)
CODE SNIPPET OUTPUT
# This method searches for the first occurrence of the specified string.
# If the specified value is not found, the partition() method returns a tuple containing:
1 - the whole string, 2 - an empty string, 3 - an empty string.
String Functions
split() : The split() method splits a string into a list. You can
specify the separator (default separator is any whitespace).
Syntax : string.split(separator, maxsplit)
CODE SNIPPET
OUTPUT
separator - Optional. Specifies the separator to use when splitting the string. By
default any whitespace is a separator.
maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is
"all occurrences"
String Functions - split()
CODE SNIPPET
OUTPUT
separator - Optional. Specifies the separator to use when splitting the string. By
default any whitespace is a separator.
maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is
"all occurrences"

String.pdf

  • 1.
    LECTURE – STRINGS BY:ABHISHEK BHARDWAJ PGT- COMPUTER SCIENCE
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
    String Operators Character ASCIICode A 65 B 66 a 97 b 98
  • 7.
  • 8.
  • 9.
  • 10.
    Length of theString & Printing String Character by Character (Traversing a String using Loop) CODE SNIPPET OUTPUT CODE SNIPPET OUTPUT
  • 11.
    Capitalize String’s FirstCharacter CODE SNIPPET OUTPUT OUTPUT From the above Output, it is clear that capitalize() only changes the String shown in output, but it does not change the Original String.
  • 12.
    Program to FindReverse a String (Method 1 : String Slicing) CODE SNIPPET OUTPUT Start from Backward Index i. e. -1 Index End not defined means default till end i. e. 0 Index Step Value i.e. -1 Decrease by 1 in each step
  • 13.
    Program to FindReverse a String (Method 2 : Programming Capabilities) CODE SNIPPET OUTPUT Length of String -1 means last character of string End is defined as -1 because it is taken as one less than actual value i.e. 0 Step Value i.e. -1
  • 14.
    Program to CountTotal Vowels and Consonants in a String CODE SNIPPET Start from 0 Index Step Value by default will increase by 1 in each step OUTPUT End is defined as end of string index
  • 15.
  • 16.
    Program to FindLowest Index in the String where Substring is found CODE SNIPPET Start from 0 Index End Index one less from the string length OUTPUT Substring to Find
  • 17.
    Program to Checkwhether given String is Palindrome or Not? CODE SNIPPET Step Value i.e. -1 Decrease by 1 in each step OUTPUT Start from Backward Index i. e. -1 Index End not defined means default till end i. e. 0 Index
  • 18.
  • 19.
  • 20.
  • 21.
    String Functions title() :Make the first letter in each word upper case. CODE SNIPPET OUTPUT lower() : Lower case the string CODE SNIPPET OUTPUT
  • 22.
    String Functions upper() :Upper case the string. CODE SNIPPET OUTPUT count() : Return the number of times a specified value appears in the string. CODE SNIPPET OUTPUT
  • 23.
    String Functions index() :The index() method finds the first occurrence of the specified value. The index() method raises an exception if the value is not found . CODE SNIPPET OUTPUT
  • 24.
    String Functions endswith() :The endswith() method returns True if the string ends with the specified value, otherwise False. Syntax : string.endswith(value, start, end). CODE SNIPPET OUTPUT
  • 25.
    String Functions startswith() :The startswith() method returns True if the string starts with the specified value, otherwise False. Syntax : string.startswith(value, start, end) CODE SNIPPET OUTPUT
  • 26.
    String Functions isalpha() :The isalpha() method returns True if all the characters are alphabet letters (a-z). Syntax : string.isalpha() CODE SNIPPET OUTPUT
  • 27.
    String Functions islower() :The islower() method returns True if all the characters in the text are in lower case, otherwise False. Numbers, symbols and spaces are not checked. Syntax : string.islower() CODE SNIPPET OUTPUT
  • 28.
    String Functions isupper() :The isupper() method returns True if all the characters in the text are in upper case, otherwise False. Numbers, symbols and spaces are not checked. Syntax : string.isupper() CODE SNIPPET OUTPUT
  • 29.
    String Functions lstrip() :The lstrip() method removes any leading characters. (space is the default leading character to remove) Syntax : string.lstrip(characters) CODE SNIPPET OUTPUT
  • 30.
    String Functions rstrip() :The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove. Syntax : string.rstrip(characters) CODE SNIPPET OUTPUT
  • 31.
    String Functions strip() :The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters. (space is the default leading character to remove) Syntax : string.strip(characters) CODE SNIPPET OUTPUT
  • 32.
    String Functions replace() :The replace() method replaces a specified phrase with another specified phrase. Syntax : string.replace(oldvalue, newvalue, count) CODE SNIPPET OUTPUT
  • 33.
    String Functions join() :The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator. Syntax : string.join(iterable)  Any iterable object where all the returned values are strings CODE SNIPPET OUTPUT CODE SNIPPET OUTPUT When using a dictionary as an iterable, the returned values are the keys, not the values.
  • 34.
    String Functions partition() :The partition() method searches for a specified string, and splits the string into a tuple containing three elements.  The first element contains the part before the specified string.  The second element contains the specified string.  The third element contains the part after the string. Syntax : string.partition(value) CODE SNIPPET OUTPUT # This method searches for the first occurrence of the specified string. # If the specified value is not found, the partition() method returns a tuple containing: 1 - the whole string, 2 - an empty string, 3 - an empty string.
  • 35.
    String Functions split() :The split() method splits a string into a list. You can specify the separator (default separator is any whitespace). Syntax : string.split(separator, maxsplit) CODE SNIPPET OUTPUT separator - Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator. maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"
  • 36.
    String Functions -split() CODE SNIPPET OUTPUT separator - Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator. maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"