String in Python
Generally, Strings are amongst the most common types in Python programming language. We can make Strings easily by enclosing characters in quotes and Python handles single quotes the equivalent of double quotes, which makes programming very easy. Creating strings is as straightforward as assigning a value to a variable.
var1 = 'Hello flocks!'
var2 = "Python Programming is easy because of strings types"
How to access values in Python:
Here Python language does not support a character type sequence, and these are considered as strings of length that is one, therefore considered a sub string in python language.
In order to access sub strings, you have to use the square brackets for slicing (cutting) along with the index to get the sub string.
var1 = 'Hello World in python!'
var2 = "Python Programming is great"
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
print "Happy codding"
How to update String in python:
Python gives you the facility that you can update a current string by reassigning a variable to another string by which you can update the variable. The latest value can be linked to its earlier value or a totally different string completely.
var1 = 'Hell go!'
print "Updated String :- ", var1[:5] + 'Python'
print "Happy codding"
Example related to strings:
# Python Program Creation of String with different flavor
# Creating a String
with a single Quotes
String1str = 'Welcome to the World'
print("String with Single Quotes: ")
print(String1str)
# Creating a String
with a double Quotes
String1str = "I'm a human"
print("nString with Double Quotes: ")
print(String1str)
# Creating a String
with a triple Quotes
String1str = '''I'm a good boy and I live in "India"'''
print("nString with Triple Quotes: ")
print(String1str)
# Creating String with a triple
Quotes that allows multiple lines
String1str = '''India
is my
country'''
print("nCreating a multiline String: ")
print(String1str)
print "Happy codding"
