Strings!

When want to print strings, we can just use single quote or double quote.

eg., print("Hello World")
print('Hello World')

When we don't want to interpret the escape sequences, we can prefix the string with "r". This means raw string and would not interpret escape sequences.

Usage of triple quotes... using triple quotes, we can print the multi lines as follows.

print("""This is multiline
text. Hence, this needs
to be printed as it is
as multline""")


Python strings cannot be changed;

word="Python"
word[0]="T" #Not possible
Error: str object does not support item assignment

String indices:

Starts from 0 to length-1
Also, we can traverse from last to first using negative indices starting from -1 to -length

Slicing:

Strings can be sliced.. slicing is nothing but getting substring using indices


eg.,

word[2:] -> This will print the word starting from 3rd character till the end;
word[:n] -> This will print the word starting from 0th character till n-1th character
word[-1:] -> -1 refer the last character in the string, since there is no other character after that on its right, this will return just a last character
word[-4:] ->thon
word[:-4} ->Py


Comments

Popular posts from this blog

Day 2: Python Installation and IDE