This document discusses various string operations in Python including: finding the length of a string; accessing and slicing characters; the difference between strings and lists; converting case; checking character types; splitting strings; finding substrings; reading and printing strings; concatenation and repetition; iterating through strings with for loops; and common string methods like isalpha, isdigit, lower, upper, title, join, split, count, find, index. It also provides examples of problems involving anagrams, pangrams, unique characters, and removing duplicates from strings.
1 of 36
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.
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
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.
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.
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.
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.