Ezekiel Pedro
zeepy blog

zeepy blog

Strings In Python

Strings In Python

Ezekiel Pedro's photo
Ezekiel Pedro
·Nov 25, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

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.

Screenshot (159).png

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".

Screenshot (153).png

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: Screenshot (164).png

String Concatenation

Two or more strings can be combined, or concatenated, using the + operator: Screenshot (166).png

 String Length

To get the length of a string, use the len() function.

Screenshot (174).png

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:

Screenshot (168).png

The following figure shows the index for each character of the string "apple pie":

Screenshot (170).png

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:

Screenshot (172).png

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:

Screenshot (176).png

The following figure shows the negative index for each character in the string "apple pie":

Screenshot (178).png

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:

Screenshot (180).png

  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.

Screenshot (183).png

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:

Screenshot (185).png

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:

Screenshot (187).png

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:

Screenshot (189).png

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":

Screenshot (191).png

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:

Screenshot (193).png

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.

Share this