Vipin | Sun, 14 Jun, 2020 | 160
String in python are surrounded by either single quotation marks, or double quotation marks.
"Hello" or 'Hello' , both the strings literals
You can display a string literal with the
string = "Hello World" print(string)
Multiline Strings in python can be delcared using ''' '''.
string = ''' this is multiline String. ''' print(string)
Note: in the result, the line breaks are inserted at the same position as in the code.
Like other programming languages in python also strings are arrays of bytes representing unicode characters.
However, Python does not have a character data type, a single character is simply a string with a length of 1.
Square brackets can be used to access elements of the string.
string = "Hello world" print(string) # H
In Python String slicing is done using slice operator i.e. [::]
[start_index : end_index : step]
string = "Hello World" string1 = string[0:5]
In Python uses negative indexing also to access the string part and slice the string.
Negative indexing start from the last character with index value -1.
a = "SelfCode" b = a[-5:-1] print(b) #Code
To get the length of a string, use the
len() function returns the length of a string:
a = "Hello,World!" print(len(a))