02-Strings
02-Strings
___
Content Copyright by Pierian Data
1 Strings
Strings are used in Python to record text information, such as names. Strings in Python are actually
a sequence, which basically means Python keeps track of every element in the string as a sequence.
For example, Python understands the string ”hello’ to be a sequence of letters in a specific order.
This means we will be able to use indexing to grab particular letters (like the first letter, or the
last letter).
This idea of a sequence is an important one in Python and we will touch upon it later on in the
future.
In this lecture we’ll learn about the following:
1.) Creating Strings
2.) Printing Strings
3.) String Indexing and Slicing
4.) String Properties
5.) String Methods
6.) Print Formatting
[2]: 'hello'
1
[4]: # We can also use double quote
"String built with double quotes"
The reason for the error above is because the single quote in I’m stopped the string. You can use
combinations of double and single quotes to get the complete statement.
[6]: "Now I'm ready to use the single quotes inside a string!"
[6]: "Now I'm ready to use the single quotes inside a string!"
2
Hello World 1
Hello World 2
Use
to print a new line
[11]: 11
Python’s built-in len() function counts all of the characters in the string, including spaces and
punctuation.
[13]: #Check
s
Hello World
Let’s start indexing!
[15]: # Show first element (in this case a letter)
s[0]
[15]: 'H'
[16]: s[1]
3
[16]: 'e'
[17]: s[2]
[17]: 'l'
We can use a : to perform slicing which grabs everything up to a designated point. For example:
[18]: # Grab everything past the first term all the way to the length of s which is␣
↪len(s)
s[1:]
[20]: 'Hel'
Note the above slicing. Here we’re telling Python to grab everything from 0 up to 3. It doesn’t
include the 3rd index. You’ll notice this a lot in Python, where statements and are usually in the
context of “up to, but not including”.
[21]: #Everything
s[:]
[22]: 'd'
We can also use index and slice notation to grab elements of a sequence by a specified step size
(the default is 1). For instance we can use two colons in a row and then a number specifying the
frequency to grab elements. For example:
4
[24]: # Grab everything, but go in steps size of 1
s[::1]
[25]: 'HloWrd'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[29], line 2
1 # Let's try to change the first letter to 'x'
----> 2 s[0] = 'x'
Notice how the error tells us directly what we can’t do, change the item assignment!
Something we can do is concatenate strings!
[30]: s
5
[32]: # We can reassign s completely though!
s = s + ' concatenate me!'
[33]: print(s)
[34]: s
[36]: letter*10
[36]: 'zzzzzzzzzz'
6
[40]: ['Hello', 'World', 'concatenate', 'me!']
[41]: # Split by a specific element (doesn't include the element that was split on)
s.split('W')
There are many more methods than the ones covered here. Visit the Advanced String section to
find out more!
[42]: 'Insert another string with curly brackets: The inserted string'
abrackdabra
Sample Input
ABCDCDC CDC
Sample Output
2
[5]: def count_substring(string, sub_string):
count = 0
l = list(string)
for i in range(0, len(l)):
st = ""
for j in range(0, len(sub_string)):
k = (i+j)
if k < len(l):
st += l[k]
if st == sub_string:
count += 1
return count
if __name__ == '__main__':
7
string = input().strip()
sub_string = input().strip()
2
ABCDEFGHIJKLIMNOQRSTUVWXYZ 4
ABCD EFGH IJKL IMNO QRST UVWX YZ
[ ]: import textwrap
er = ""
for i in ll:
er += i + "\n"
return er
if __name__ == '__main__':
string, max_width = input(), int(input())
result = wrap(string, max_width)
print(result)
We will revisit this string formatting topic in later sections when we are building our projects!