
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).
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).
In this post we'll learn about the following:
# Single word
"Hello"
# Entire phrase
'This is also a string'
# We can also use double quote
"String built with double quotes"
# Be careful with quotes!
' I'm using single quotes, but this will create an error' #SyntaxError: invalid syntax
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.
"Now I'm ready to use the single quotes inside a string!"
Now let's learn about printing strings!
# Note that we can't output multiple strings this way
'Hello World 1'
'Hello World 2'
We can use a print statement to print a string.
print('Hello World 1')
print('Hello World 2')
print('Use \n to print a new line')
print('\n')
print('See what I mean?')
Output:
Hello World 1 Hello World 2 Use to print a new line See what I mean?
len('Hello World')
Output:-
11
Python's built-in len() function counts all of the characters in the string, including spaces and punctuation.
# Assign s as a string
s = 'Hello World'
#Check
s
Output:-
'Hello World'
Let's start indexing!
We can use a : to perform slicing which grabs everything up to a designated point. For example:# Show first element (in this case a letter) s[0]Output:- 'H's[1]Output:- 'e'
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".# Grab everything past the first term all the way to the length of s which is len(s) s[1:]Output:- 'ello World'# Note that there is no change to the original s sOutput:- 'Hello World'# Grab everything UP TO the 3rd index s[:3]Output:- 'Hel'
#Everything s[:]Output:- 'Hello World' We can also use negative indexing to go backwards.# Last letter (one index behind 0 so it loops back around) s[-1]Output:- 'd'
#Everything
# Grab everything but the last letter
s[:-1]
Output:-
'Hello Worl'
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:
# Grab everything, but go in step sizes of 2
s[::2]
Output:-
'HloWrd'
# We can use this to print a string backwards
s[::-1]
Output:-
'dlroW olleH'
s = "Hello World"
# Let's try to change the first letter to 'x'
s[0] = 'x'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
1 # Let's try to change the first letter to 'x'
----> 2 s[0] = 'x'
TypeError: 'str' object does not support item assignment
Notice how the error tells us directly what we can't do, change the item assignment!
Something we can do is concatenate strings!
s = "Hello World"
# Concatenate strings!
s + ' concatenate me!'
# We can reassign s completely though!
s = s + ' concatenate me!'
Output:-
'Hello World concatenate me!'
We can use the multiplication symbol to create repetition!
letter = 'z'
letter*10
Output:-
'zzzzzzzzzz'
s="Hello World"
# Upper Case a string
s.upper()
Output:-
"HELLO WORLD"
s="Hello World"
# Lower case
s.lower()
# Split a string by blank space (this is the default)
s.split()
Output:-
"hello world"
['Hello', 'World', 'concatenate', 'me!']
# Split by a specific element (doesn't include the element that was split on)
s.split('W')
Output:-
['Hello ', 'orld concatenate me!']
There are many more methods than the ones covered here. Visit the Advanced String section to find out more!
'Insert another string with curly brackets: {}'.format('The inserted string')
Output:-
'Insert another string with curly brackets: The inserted string'
Thanks For Reading
Sign in to join the discussion and post comments.
Sign in