String Literals
String
literals in python are surrounded by either single quotation marks, or double
quotation marks.
'hello' is
the same as "hello".
You can
display a string literal with the print() function
#You can use double or single quotes:
print("Hello youtube")
print('Hello youtube')
Slicing
You can
return a range of characters by using the slice syntax.
Specify
the start index and the end index, separated by a colon, to return a part of
the string.
Example
Get
the characters from position 2 to position 5 (not included):
b
= "Hello, World!"
print(b[2:5])
Negative Indexing
Use negative indexes to start the slice from the end of the string:
Example
Get
the characters from position 5 to position 1, starting the count from the end
of the string:
b
= "Hello, World!"
print(b[-5:-2])
String Length
To get the length of a string, use the len()
function.
Example
The len()
function
returns the length of a string:
a
= "Hello, World!"
print(len(a))
No comments:
Post a Comment