DEV Community

Cover image for L3 - STRINGS
Logical-Developer-Gaurav
Logical-Developer-Gaurav

Posted on

L3 - STRINGS

**STRINGS ARE IMMUTABLE DATATYPE IN PYTHON **

CONCATENATION OF STRING

8.png

STRING SLICING

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

9.png

How to slice a given string

*SYNTAX - * name_of_variable[ start : end : step ]

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

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

name_of_string.endswith()

This function tells whether the variable string ends with the given string or not.
*It returns a boolean *

13.png

name_of_string.count()

Counts the total no of occurence of any character.

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

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

name_of_string.replace(oldword,neword)

This function replaces the old word with the new word in the entire string.

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

17.2.png

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

\t - tab space escape sequence

\t means a tab space

19.png

\' and \"- single quotes and double quotes escape sequence

\' means single quote.
\" means double quote.

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

Top comments (0)