2. Python String
Strings in python are surrounded by either single quotation marks, or double
quotation marks.
'hello' is the same as "hello".
Ex print("Hello")
Output: Hello
Assign string to a variables
For ex:
A=“hello”
Print(a)
Output:
Hello
3. Multiline Strings
You can assign a multiline string to a variable by using three quotes
For ex.
A=“”” Essay are short pieces of writing with a focus on a single subject. It starts with
the introduction of the subject, followed by a middle or body in which the author
gives his argument as per his understanding of the subject followed by a conclusion
paragraph that summarises the findings of the essay.”””
Print(a)
In single quotes:
For ex.
a=‘’’Essay are short pieces of writing with a focus on a single subject. It starts with the
introduction of the subject, followed by a middle or body in which the author gives his
argument as per his understanding of the subject followed by a conclusion paragraph
that summarises the findings of the essay.’’’
Print(a)
4. Strings as array
Square brackets can be used to access elements of the string.
For ex.
A=“hello world”
Print(a[1])
Output:
e
Slicing string
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.
5. For ex.
b = "Hello, World!"
print(b[2:5])
Output:
llo
Modify Strings
Upper Case for ex. a=“hello world” print(a.upper())
Lower Case for ex. A=“hello world” print(a.lower())
Remove Whitespace for ex. A=“hello world” print(a.strip())
Replace String for ex. A=“hello world” print(a.replace())
String Concatenation
For ex.
a = "Hello"
b = "World"
c = a + b
print(c)