What are Python Strings? How to Create String in Python?
What are Strings in Python?
Strings are text fragments. They can be defined as anything that falls between two quotation marks:
astring = "Welcome to WSC!"
astring1 = Welcome to WSC'
astring2 = “”” Welcome to WSC “””
As you can see, you learned how to print a basic sentence initially. Python has saved this phrase as a string. Instead of printing strings out right away, we'll look at the many things you can do with them.
To allocate a string, you can alternatively use single quotes. If the value to be allocated contains single quotes, you will run into issues. To assign the string in these brackets (single quotes are"), for example, you must only use double quotes like this.
astring = "Hello WSC!"
print("single quotes ' '")
print(len(astring))
Output:
Test your knowledge with a quick quiz!
-
What would be the result of the below Python statement?
What would be the result of the below Python statement?
"a1"+"bc"
Select the correct answer
String Functions in Python
Python provides several built-in techniques for working with strings.
Each string method generates a new value. They don't alter the original string in any way.
In general, even though the strings appear to be numbers, you can’t execute mathematical operations on them.
The following are prohibited (assuming that the message is of type string):
message - 1
"WSC" / 123
message * "WSC"
"15" + 2
Surprisingly, the + operator can be used with strings, although it indicates concatenation rather than addition. Concatenation is the process of combining two operands by connecting them end-to-end. Consider the following scenario:
fruit = "melon"
bakedGood = " nut bread"
print(fruit + bakedGood)
Output:
Melon nut bread is the result of this program. The space preceding the word nut is part of the string and is required for the gap between the concatenated strings to be created.
The * operator may be used on strings as well. It goes through the motions of repetition. 'Fun'*3 is, for example, 'FunFunFun'. The first operand should be a string, and the second must be an integer.
For example:
print("WSC" * 6)
name = "Python"
print(name * 3)
print(name + "WSC" * 3)
print((name + "WSC") * 3)
Output:
How to Create a String in Python?
In Python, single quotes, double quotes, and even triple quotes can be used to generate strings.
String1 = 'Welcome to WSC'
print("String with Single Quotes: ")
print(String1)
String1 = "I love WSC"
print("\nString with Double Quotes: ")
print(String1)
print(type(String1))
String1 = '''Best Python Tutorial"'''
print("\nString with Triple Quotes: ")
print(String1)
print(type(String1))
String1 = '''WSC
For
Python'''
print("\nCreating a multiline String: ")
print(String1)
Output:
Here are the index values for the Violet:
The negative index values for Violet:
Accessing String Elements in Python
You can use the index numbers to retrive the list elements. Negative address references, such as -1 for the final character, -2 for the second last character, and so on, enable negative address references to access characters from the back of the String.
String1 = "WSC"
print("Initial String: ")
print(String1)
# Printing First character
print("\nFirst character of String is: ")
print(String1[0])
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])
Output: