In Python, strings are sequences of characters, surrounded by single (‘ ‘) or double (” ” ) quotation marks. For example "Hello World!"
and 'Hello World!'
are same.
To display a string literal we will use a print() function, as we have learnt in our previous article.
Assigning string to a variable is super easy in Python. Just use the variable name followed by an equal sign and a string. For Example:
a = "Hello World!"
print (a) # outputs: Hello World!
Multiline strings are assign to a variable by using the three double quotes. For Example;
a = """Some line of text,
Also some line of text,
let's write random lines of text once more,
Now this is the last one."""
Print (a)
Output
Some line of text,
Also some line of text,
let’s write random lines of text once more,
Now this is the last one.
NOTE:- You can also use three single quotes (”’) instead of three double quotes (“””) for multiline strings.
String concatenation means combining two or more strings together. Strings in Python can be combined by using the “+” operator. For Example:
a = "Hello " b = "World!" c = a + b print (c) # Outputs: Hello World!
In Python, Strings can be accessed using index notation. For example:
a = "Hello, World!" print(a[0]) # Outputs: "H" print(a[4]) # Outputs: "o" print(a[0:5]) # Outputs: "Hello"
Code Explanation
In an above code, we use indexing to access the characters of the string. Index numbers starts with 0 not 1. We also use slice syntax to get the range of characters from 0 to 5, means first five characters of the string.
We can calculate the length of the string by using the len() function. For Example:
a = "Hello World" print (len(a)) # outputs: 11
We can convert our string in lowercase or uppercase by using the lower()
and upper()
methods. For Example:
a = "Hello World" print (a.lower()) # outputs: hello world print(a.upper()) # outputs: HELLO WORLD
In order to check if a certain character or word is present in a string or not, we can use the keyword "in"
. In this case the output will be either True or False. For Example:
a = "Hello World we are programmers" print ("are" in a) # outputs: True print ("o" in a) # outputs: True print ("b" in a) # outputs: False print ("high" in a) # outputs: False
String formatting is a powerful feature of Python that allows us to create dynamic strings that can include variables, expressions, and other data types. There are several ways to format strings in Python, including the %
operator and the format()
method.
The %
operator is used to substitute values into a string template. For example:
name = "John" age = 25 a = "My name is %s and I am %d years old." print (a %(name,age)) # Output: "My name is John and I am 25 years old."
In an above code, the %s
and %d
placeholders are replaced with the values of the name
and age
variables.
The format()
method is a more flexible way to format strings in Python. For example:
name = "David" age = 28 print("My name is {} and I am {} years old.".format(name, age)) # Output: "My name is David and I am 28 years old."
Share This Post!