Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Python strings
 Find length of string
 Accessing individual characters/slicing
 Difference between string and list.
 Convert to lowercase / uppercase / titlecase /
swapcase
 Verify if the strings consists of
alphabets/digits/upper/lower
 Split string
 Find substring
 A string is a sequence of characters.
 We can create string by enclosing characters in quotes.
 Python treats single quotes the same as double quotes.
 Python uses Unicode format to represent characters.
 Reading and Printing a string
 We can access individual characters of
string using indexing.
 If we try to access index out of the range or use
decimal number, we will get errors.
 Since there is no separate “character” type, indexing a
string produces strings of length 1
 We can access substrings using slicing
 Joining of two or more strings into a single
one is called concatenation.
 The + operator does this in Python.
 The * operator can be used to repeat the
string for a given number of times.
 Using for loop we can iterate through a
string.
Python strings
str.isalnum()
str.isalpha()
str.isdigit()
str.islower()
str.isspace()
str.isupper()
str.istitle()
Python strings
 Write code to check if the given password is strong or not.
str.lower() Convert all characters to lowercase
str.upper() Convert all characters to uppercase
str.swapcase() Convert uppercase to lowercase and
vice versa
str.title() First char of each word is changed to
uppercase and others to lowercase
str.capitalize() First char of the string is changed to
uppercase others to lowercase
Python strings
 You are given a string and your task is to swap cases. In other words,
convert all lowercase letters to uppercase letters and vice versa.
 Count the number of vowels in a string.
str.count(sub) Return the number of non-overlapping occurrences of
substring sub .
str.find(sub) Return the lowest index in the string where substring sub is
found. Return -1 if sub is not found.
str.index(sub) Like find(), but raiseValueError when the substring is not found.
str.split(sep = None) Return a list of the words in the string, using sep as the
delimiter string.
S.join(list) Return a string which is the concatenation of the strings in the
list. The separator between elements is S.
Python strings
Count number of overlapping substrings
Python strings
 Return a list of the words in String
 Default separator is any space(space,newline,tab)
 To specify any other separator, specify it explicitly.
 Used to efficiently construct strings from multiple
fragments.
 str.join(iterable)
 Return a string which is the concatenation of the strings
in iterable.
 The separator between elements is the string providing
this method.
 A TypeError will be raised if there are any non-string
values in iterable.
 vowels = ['a', 'e', 'i', 'o', 'u']
 vowels
 ['a', 'e', 'i', 'o', 'u']
 s = "-".join(vowels)
 s
 'a-e-i-o-u'
 nums = [12,23,21,43,56]
 nums
 [12, 23, 21, 43, 56]
 s = "".join(nums)
 TypeError: sequence item 0: expected str
instance, int found
 s = "".join(map(str , nums) )
 s
 '1223214356'
 Replace method
Python strings
 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 once.
 Anagram Example:
LISTEN SILENT
BINARY BRAINY
SCHOOL MASTER THE CLASSROOM
CAR ARC
 Given two strings, verify if they are anagram
Python strings
 Pangram is a sentence containing every letter of
the alphabet.
 Given a sentence, determine whether it is
pangram, ignore case.
 Pangram Examples:
 The quick brown fox jumps over the lazy dog
 Two driven jocks help fax my big quiz.​
 Pack my box with five dozen liquor jugs.
 The five boxing wizards jump quickly.
 Bright vixens jump; dozy fowl quack.
Python strings
 Write a program to verify if the given string has all
unique characters
 Write a program to remove all duplicate characters
from a string.
Python strings

More Related Content

Python strings

  • 2.  Find length of string  Accessing individual characters/slicing  Difference between string and list.  Convert to lowercase / uppercase / titlecase / swapcase  Verify if the strings consists of alphabets/digits/upper/lower  Split string  Find substring
  • 3.  A string is a sequence of characters.  We can create string by enclosing characters in quotes.  Python treats single quotes the same as double quotes.  Python uses Unicode format to represent characters.
  • 4.  Reading and Printing a string
  • 5.  We can access individual characters of string using indexing.  If we try to access index out of the range or use decimal number, we will get errors.
  • 6.  Since there is no separate “character” type, indexing a string produces strings of length 1
  • 7.  We can access substrings using slicing
  • 8.  Joining of two or more strings into a single one is called concatenation.  The + operator does this in Python.  The * operator can be used to repeat the string for a given number of times.
  • 9.  Using for loop we can iterate through a string.
  • 13.  Write code to check if the given password is strong or not.
  • 14. str.lower() Convert all characters to lowercase str.upper() Convert all characters to uppercase str.swapcase() Convert uppercase to lowercase and vice versa str.title() First char of each word is changed to uppercase and others to lowercase str.capitalize() First char of the string is changed to uppercase others to lowercase
  • 16.  You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.
  • 17.  Count the number of vowels in a string.
  • 18. str.count(sub) Return the number of non-overlapping occurrences of substring sub . str.find(sub) Return the lowest index in the string where substring sub is found. Return -1 if sub is not found. str.index(sub) Like find(), but raiseValueError when the substring is not found. str.split(sep = None) Return a list of the words in the string, using sep as the delimiter string. S.join(list) Return a string which is the concatenation of the strings in the list. The separator between elements is S.
  • 20. Count number of overlapping substrings
  • 22.  Return a list of the words in String
  • 23.  Default separator is any space(space,newline,tab)  To specify any other separator, specify it explicitly.
  • 24.  Used to efficiently construct strings from multiple fragments.  str.join(iterable)  Return a string which is the concatenation of the strings in iterable.  The separator between elements is the string providing this method.  A TypeError will be raised if there are any non-string values in iterable.
  • 25.  vowels = ['a', 'e', 'i', 'o', 'u']  vowels  ['a', 'e', 'i', 'o', 'u']  s = "-".join(vowels)  s  'a-e-i-o-u'
  • 26.  nums = [12,23,21,43,56]  nums  [12, 23, 21, 43, 56]  s = "".join(nums)  TypeError: sequence item 0: expected str instance, int found  s = "".join(map(str , nums) )  s  '1223214356'
  • 29.  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 once.  Anagram Example: LISTEN SILENT BINARY BRAINY SCHOOL MASTER THE CLASSROOM CAR ARC
  • 30.  Given two strings, verify if they are anagram
  • 32.  Pangram is a sentence containing every letter of the alphabet.  Given a sentence, determine whether it is pangram, ignore case.  Pangram Examples:  The quick brown fox jumps over the lazy dog  Two driven jocks help fax my big quiz.​  Pack my box with five dozen liquor jugs.  The five boxing wizards jump quickly.  Bright vixens jump; dozy fowl quack.
  • 34.  Write a program to verify if the given string has all unique characters
  • 35.  Write a program to remove all duplicate characters from a string.