String
- In computer programming, a string is a sequence of characters.
- The variable is initialized with the string Python Programming.
- The string is collection of alphanumeric letters and special characters.
- A string is a collection of alphabets ('a' to 'z' and 'A' to 'Z'),numbers (0 to9),spaces and spe6 characters.
- string value is enclosed by single or double or triple quotes The flexibility of using different quotes is to use the other quotes as part of the string value.
- The main restriction is same quote should be used at beginning and end of the string to denote the string value.
String Usage:
Strings are used for storing text/characters.
Example:
"Hello World" is a string of
characters.
Types Of String:
- Char
- Varbinary
- Blob
- Text
- Set
- Enum
Creating Strings
- A string in python can be created using single or double or even triple quotes
- String in single quotes cannot hold any other single quotes character in it, because the compiler will not recognize where to start and end the string.
- To overcome this problem you have to use double quotes.
- String which contains double quotes should be define within triple quotes Defining strings within triple quotes also allows creation of multiline strings.
Program:
# Prompt the user to enter a string
input_str = input("Enter your name: ")
# Print the string to the console
print("Your name is :", input_str)
# Prompt the user to enter a string
input_str = input("Enter your name: ")
# Print the string to the console
print("Your name is :", input_str)
Output:
Enter your name: Siva
Your name is :Siva
Enter your name: Siva
Your name is :Siva
String And String Array
- String is a data type in python ,which is used to handle array of characters.
- String is a sequence of Unicode characters that may Be combination of letters, numbers, or special symbols enclosed within single ,double or even triple quotes. strings are immutable ,it means, once you define a string, it cannot be changed during execution.
- String Array can be defined as the capacity of a variable to contain more than one string value at the same time, which can be called and accessed at any time in the program during the execution process.
- A String Array is an Array of a fixed number of String values.
- A String is a sequence of characters. Generally, which means the value of the string can not be changed.
- The String Array works similarly to other data types of Array. In Array, only a fixed set of elements can be stored.
- The key difference between Array and String is that an Array is a data structure.
Program:
# Create an array of strings
string_array = ["apple", "banana", "cherry", "elderberry"]
# Print each string in the array to the console
for string
in string_array:
print(string)
Output:
apple
banana
cherry
elderberry
More topic in Python