# Strings In Python

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669295351428/REDkBoLn_.png align="left")

    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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669294050691/0yHZlvnE6.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669298049134/qy3Qfyr9s.png align="left")

    String Concatenation
Two or more strings can be combined, or concatenated, using the + operator:
![Screenshot (166).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669298953391/pVuujyC_L.png align="left")

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

![Screenshot (174).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669301066925/7oc7dYU96.png align="center")

    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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669300018064/5C8vzgpST.png align="left")

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

![Screenshot (170).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669300433475/nPSidd09U.png align="center")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669300649660/dLBf6Hn-q.png align="center")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669302043409/sp2W__a22.png align="left")

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

![Screenshot (178).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669302226529/ZHsh06pmc.png align="center")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669302532734/k-lA0sMmf.png align="center")

      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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669303037540/wZ7s4zrj8.png align="center")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669303388338/A1jM01ufo.png align="center")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669303711963/EmPd7wkma.png align="center")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669304393230/S3YKJ5SvM.png align="center")


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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669305414250/lFxEH6SiN.png align="center")

    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](https://cdn.hashnode.com/res/hashnode/image/upload/v1669305675300/dPMFfl8zk.png align="center")

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.


