L3 - STRINGS
Tags: beginners, tutorial, programming, python
Published: 2022-04-28 13:43:45




***STRINGS ARE IMMUTABLE DATATYPE IN PYTHON ***

# CONCATENATION OF STRING

![8.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649262681748/1_t-a4zjU.png)

# STRING SLICING
## How to get the character of a given index in a string in python

![9.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649264764214/3rFl5IHef.png)
## How to slice a given string
**SYNTAX - **  name_of_variable[ start : end : step ]



![10.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649268525600/HSwHZPDFE.png)

**NOTES - **

- 
If nothing is written in place of start , then the function will start slicing from the index = 0 

- If nothing is written in place of end , then the function will do slicing till the end of the string.
- If nothing is written in place of step , then the function will print every character.


**NOTE -> WE CAN ONLY ACCESS THE CHARACTERS OF A STRING AND CANNOT CHANGE THE CHARACTER OF A STRING USING THIS FUNCTION !   IF YOU TRY DO SO THEN IT WILL SHOW ERROR AS SHOWN HERE**


![11.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649269150538/lm-U_SDeV.png)

***We are not able to do so because strings are immutable datatype in python.***

# STRING FUNCTIONS 
## len() function 
This function returns the length of the string

![12.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649270049737/qHsBPTt78.png)


## name_of_string.endswith()
This function tells whether the variable string ends with the given string or not. 
*It returns a boolean *

![13.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649270392745/SlBWtxwIg.png)

## name_of_string.count()
Counts the total no of occurence of any character.

![14.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649270754673/0TYvO_95M.png)

## name_of_string.capitalize()
It capitalizes the first letter of the given string.

**NOTE - **
It also makes all other letters (except first letter) in the string to lowercase.

![15.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649304356113/XblJklQ4F.png)

## name_of_string.find(word)
This function finds a word and returns the index of the first occurence of that word in the string.

![16.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649305384293/26SDShsXZ.png)

## name_of_string.replace(oldword,neword)
This function replaces the old word with the new word in the entire string.

![17.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649305674081/_6EPnm7Od.png)

##  All string methods returns new values. They do not change the original string
You can see this in the examples given below.

![17.1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649492384394/0vC7a9OSg.png)


![17.2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649492398122/w0CUcJ51X.png)


![17.3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649492408486/K3bcxLeUW.png)







# ESCAPE SEQUENCE CHARACTER
The programmers refer to the "backslash (\)" character as an escape character. In other words, it has a special meaning when we use it inside the strings. As the name suggests, the escape character escapes the characters in a string for a brief moment to introduce unique inclusion. That is to say; backlash signifies that the next character after it has a different meaning. 

\ is the escape character in Python. Additionally, it announces that the next character has a different meaning.
Moreover, the set of characters after , including it, is known as an escape sequence.

Escape sequence character comprises of more than one character but represents one character **when used within the strings.**


## \n - New line escape sequence
\n means a new line , we will get a new line when we'll use this in the middle of the text in print function.

![18.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649307226354/Wc5Sncsdc.png)

## \t - tab space escape sequence


\t means a tab space 

![19.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649307439200/GtXH1gBXl.png)

## \' and \"- single quotes and double quotes escape sequence
\' means single quote.
\" means double quote.


![20.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649333367445/ZzUDMZMa1.png)

##  \\\ means , \ (a backslash)
If you want a backslash to be the part of your string then you cannot do it by typing \ in the string because \ is a escape sequence character in python. 
For doing so you will have to type \\\ . \\\ means \\.

![21.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1649333632282/7n_-b_M_A.png)