In this article, I will cover strings and loops in python
What are Strings?
Strings are one of the fundamental Python data types. A string is a sequence of characters, in other words, a string is a piece of text. In Python, strings are not just sequences of characters but a single character can also be referred to as a string
Note: There are several other data types in Python e.g int, float, etc.
Python string has a special abbreviation "str" and this can be seen by using the type() function, a Python function used to determine the data type of a given value.
Strings have three (3) properties:
- Strings contain characters, which are individual letters or symbols.
- Strings have a length which is the number of characters contained in the string.
Characters in a string appear in a sequence, meaning each character has a numbered position in the string.
String Literals
Strings literals are strings with text surrounded with quotation marks. e.g 'hello' and "hello".
Do note that whenever you create a string by surrounding text with quotation marks, the string is called a string literal. The name indicates that the string is written out in your code.
Note: Not every string is a string literal. For example, a string captured as user input isn’t a string literal because it isn’t explicitly written out in the program’s code
Multiline Strings
Multiline strings can also be created using triple quotes as delimiters (""" or '''). Here is how you might write a long paragraph using this approach:
String Concatenation
Two or more strings can be combined, or concatenated, using the + operator:
String Length
To get the length of a string, use the len() function.
String Indexing
Each character in a string has a numbered position called an index. You can access the character at the Nth position by putting the number N in between two square brackets ([ and ]) immediately after the string:
The following figure shows the index for each character of the string "apple pie":
If you try to access an index beyond the end of a string, Python raises an IndexError because you can't access an index beyond the max of the string:
The largest index in a string is always one less than the string’s length. Since "apple pie" has a length of nine, the largest index allowed is 8
Strings also support negative indices:
The following figure shows the negative index for each character in the string "apple pie":
Just like positive indices, Python raises an IndexError if you try to access a negative index less than the index of the first character in the string:
String Slicing
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string.
flavor[0:5] returns the first five characters of the string assigned to flavor, starting with the character with index 0 and going up to, but not including, the character with index 5. The [0:5] part of flavor[0:5] is called a slice. In this case, it returns a slice of "apple pie"
The slice [x:y] returns the substring between the boundaries x and y. So, for "apple pie", the slice [0:5] returns the string "apple", and the slice [3:9] returns the string "le pie"
By leaving out the start index, the range will start at the first character:
The slice [:5] is equivalent to the slice [0:5], so flavor[:5] returns the first five characters in the string "apple pie"
By leaving out the end index, the range will go to the end:
For "apple pie", the slice [6:] is equivalent to the slice [6:9]. Since the character with index 6 is "p", flavor[6:9] returns the substring that starts with "p" and ends with the last letter, which is "pie".
If you omit both the first and second numbers in a slice, you get a string that starts with the character with index 0 and ends with the last character. In other words, omitting both numbers in a slice returns the entire string:
Just like before, the slice [x:y] returns the substring between the boundaries x and y. For instance, the slice [-9:-4] returns the first five letters of the string "apple pie":
Strings Are Immutable
To wrap this article up, let’s discuss an important property of string objects. Strings are immutable, which means that you can’t change them once you’ve created them. For instance, see what happens when you try to assign a new letter to one particular character of a string:
Python throws a TypeError and tells you that str objects don’t support item assignment.
Note: The term str is Python’s internal name for the string data type.