**STRINGS ARE IMMUTABLE DATATYPE IN PYTHON **
CONCATENATION OF STRING
STRING SLICING
How to get the character of a given index in a string in python
How to slice a given string
*SYNTAX - * name_of_variable[ start : end : step ]
*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
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
name_of_string.endswith()
This function tells whether the variable string ends with the given string or not.
*It returns a boolean *
name_of_string.count()
Counts the total no of occurence of any character.
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.
name_of_string.find(word)
This function finds a word and returns the index of the first occurence of that word in the string.
name_of_string.replace(oldword,neword)
This function replaces the old word with the new word in the entire string.
All string methods returns new values. They do not change the original string
You can see this in the examples given below.
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.
\t - tab space escape sequence
\t means a tab space
\' and \"- single quotes and double quotes escape sequence
\' means single quote.
\" means double quote.
\\ 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 \.
Top comments (0)