Introduction to Strings in Python
Strings in Python are sequences of characters, each character with an index. They can be manipulated with various expressions and methods. Learn about creating, manipulating, and iterating over strings, as well as common string operations like concatenation and retrieval of specific characters. Explore examples of string reversal and counting, along with string object methods. Dive into the world of strings and unlock their potential in Python programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
STRINGS IN PYTHON Peter Larsson-Green J nk ping University Autumn 2018
STRINGS Represents a sequence of characters. Expressions creating strings: "This is a string." 'This is a string.' This is a string. This is a string. """This is a string covering multiple lines.""" The + operator can be used to concatenate strings: + "This is " 'a string!' This is a string covering multiple lines. This is a string!
STRINGS "Winter" "Summer" == False False == "Winter" "winter" < "Hello" "Hi" True 4 * 'ab' "abababab" True "b" in 'abc' True "bc" in 'abc' True "cb" not in 'abc'
STRINGS ARE SEQUENCES A string is a sequence of characters. Each character in the string has an index. = abc Index: a 0 -3 b 1 -2 c 2 -1 Expression retrieving a character at specified index: [] <str-expr> <index-expr> a c "abc"[0] "abc"[2] b "abc"[1] 3 len("abc")
ITERATING OVER STRINGS name = "Alice" for c in name: print(c) name = "Alice" for i in range(len(name)): print(str(i) +" "+ name[i]) A l i c e 0 A 1 l 2 i 3 c 4 e
EXAMPLE def reverse(string): reversed = "" for c in string: reversed = c + reversed return reversed def sum(numbers): sum = 0 for n in numbers: sum = sum + n return sum reverse("abc") cba reverse("12345") 54321
STRINGS ARE OBJECTS Objects have methods. <expr> .method() Some string methods: Abc abc "abc abc".capitalize() 2 "abc abc".count("b") True "abc abc".islower() Strings are immutable.
SOME MORE STRING METHODS abc abc "AbC aBc".lower() abxxabc "abc abc".replace("c ", "xx") True "abc abc".startswith("ab") "AbC aBc".swapcase() aBc AbC "Abc abc".upper() ABC ABC help(str)
SLICING Indexes: 01234 Extracting a sub sequence of a sequence. [:] <seq-expr> name = "Alice" "Alice" name[:] [ :] <expr> <seq-expr> "ice" name[2:] [: ] <expr> <seq-expr> "Al" name[:2]
SLICING Indexes: 01234 Extracting a sub sequence of a sequence. [ : ] <seq-expr> <expr> name = "Alice" <expr> "li" name[1:3] [ : : ] <expr> <expr> <seq-expr> <expr> "lc" "ce" name[1:4:2] name[-2::] "Aie" "ci" name[::2] name[3:1:-1] "ie" "ecilA" name[2::2] name[::-1]