Module 7 Strings
Module 7 Strings
s = 'hello world'
String Slicing- Answers
1. e
2. d
3. el
4. ello worl
5. hel
6. llo world
7. hello worl
8. hlowrd
9. el ol
10. dlrow olleh
String Concatenation
You can concatenate strings using the + operator:
String Concatenation
String Length
You can find the length of a string using the len() function:
String Length
String Methods
Python provides numerous built-in methods for
manipulating strings, such as converting cases, removing
whitespaces, replacing characters, splitting, joining, and more.
String Methods
str.upper()
str.lower()
str.capitalize()
str.title()
str.strip()
str.lstrip()
str.rstrip()
str.startswith(prefix).
str.endswith(suffix)
str.replace(old, new).
String Methods
str.split(separator)
str.join(iterable)
str.find(substring)
str.rfind(substring)
str.index(substring)
str.rindex(substring)
str.count(substring)
str.isalnum()
str.isalpha()
String Methods
str.isdigit()
str.islower()
str.isupper()
str.isspace()
str.isnumeric().
str.isdecimal()
str.startswith(prefix, start, end
str.endswith(suffix, start, end)
String Formatting
Python supports multiple ways of formatting strings,
including old-style % formatting, str.format(), and f-strings
(formatted string literals).
String Formatting
Escape sequences
Special character combinations that are used to represent
characters that are otherwise difficult or impossible to include
directly in a string
Escape sequences
\\: Backslash
\': Single Quote
\": Double Quote
\n: Newline (line break)
\t: Tab
\r: Carriage Return (used for some text file formats)
\b: Backspace (moves the cursor back one space)
\f: Form Feed (used for some text file formats)
\v: Vertical Tab (rarely used)
Problems On
Strings +
Numbers +
Decision Making
Vowel Counter
Write a program that takes a string input from the user and
counts the number of vowels (A, E, I, O, U, and their lowercase
equivalents) in the string.