What are Strings ?
A string is a data type that represents a sequence of characters.
Strings are arrays of bytes denoted by Unicode characters. It can be collection of one or more characters and can be put in a single quote, double-quote or triple quote. No character data type exists in Python, a character is a string of length one. It is represented by str class.
Creating String:
Strings in Python can be created using single quotes or double quotes or even triple quotes.
#using single quotes
a = ‘Hello World’
#using double quotes
b = “Hello World”
Note: Strings are not limited to be enclosed in single or double quotes, rather you can use triple quotes as well to create a multiline string.
For example:
#single quote
a = 'Hello World'
print(a)
#double quotes
b = "Hello World"
print(b)
#triple quotes
c = """Hey, Welcome to fullstackadda.com, Learn How to code !"""
print(c)
Hello World
Hello World
Hey, Welcome to fullstackadda.com, Learn How to code !
String Operations in Python:
There are many string operations that you can do with different data types and variables in Python.
The Concatenate Operation:
When you join or ‘add’ to strings together to form one new string, that process is called concatenation. The plus(+) symbol is used to concatenate or join two strings together:
firstName = 'Surya'
lastName = 'Singam'
name = firstName + " " + lastName
print(name)
Surya Singam
Using Iteration in Strings:
You can use a for loop to through the characters in your string quite easily. For example, if you want to count the number of characters present in your string then you can simply:
#Counting the number of characters of a string using a for loop
string = 'Welcome to Fullstackadda.com'
count = 0
for letter in string:
count = count + 1
print(count)
28
How Strings are Indexed?
Like any other character in python, characters used in strings are also indexed number wise, where each character takes up a 1 byte of memory and has an address in terms of indexing. For example, if I have a string consisting of the word ‘Fullstackadda’ then each character of ‘Fullstackadda’ has a separate index and memory. Let’s say that I want to access “F” from the string ‘Fullstackadda’, then I would simply write:
Example:
string = 'Fullstackadda'
print(string[0])
F
Since, indexing always starts from 0, so the first letter will be “F” in this case which is stored at 0 index. The output of the above code will be
Positive and Negative Indexing of Strings
We have learned that strings can be indexed positively through numbers starting from 0 onward. However, strings can be indexed negatively as well in the reverse order. For example, to view the last character of the word ‘Fullstackadda’ you can enter the index “-1” to view letter n from ‘Fullstackadda’.
string = 'Fullstackadda'
print(string[-1])
a
Slicing a String:
You can also slice a string by choosing the index number of characters that you want to slice through the slicing method. Let’s slice “F” from “Fullstackadda” by omitting index ‘0’ from the rest of the index numbers of characters:
#slicing F from python
string = 'Fullstackadda'
print(string[1:13])
ullstackadda
Deleting a String:
You can delete a string too but remember that deleting a character from a string is not possible however you can delete an entire string using del function:
string = 'Hello'
del string
print(string)
Output:
Built-in Functions in Strings:
Python consists of tons of built-in functions for strings. These functions/method enable strings to produce different result for a particular query. For example, if you want to play around with the text cases of your string, you can create lower(), upper(), title() and capitalize()functions to your strings.
Some Examples :
string = "Welcome to fullstackadda.com"
#string to lowercase
print(string.lower())
#string to uppercase
print(string.upper())
#First letter of each word capital
print(string.title())
#First letter of your string capital
print(string.capitalize())
welcome to fullstackadda.com
WELCOME TO FULLSTACKADDA.COM
Welcome To Fullstackadda.Com
Welcome to fullstackadda.com
- String is a data type that represents a sequence of characters.
- Strings are not limited to be enclosed in single or double quotes.