What is a String?
In Previous blog, you created the string "Hello, World" and printed it in
IDLE’s interactive window using print(). In this blog, you will get a
detail look into what strings are and the different ways you can
create them in Python program.
The String Data Type
Strings are one of the basic Python data types. The term data
type refers to what type of data a value represents. Strings are used
to represent text.
We say that strings are a fundamental data type because they can’t
be broken down into smaller values of a different type. Not all data
types are fundamental.
'str' is used in short for string in python programming. You can see this by using type(), which is a function used to determine
the data type of a given value.
write the below code in your code editor, which shows you the class of the string:
type("Hello, World")
<class 'str'>
"Hello World" is a string, so it shows us 'str' class in the output above.
type() also works for values that have been assigned to a variable:
phrase = "Hello, World"
type(phrase)
<class 'str'>
Strings have three important properties:
1. The individual letters or symbols in string is called characters.
2. The number of character in a string is called its length.
3. Characters in a string positioned in a sequence, which means that
every character has a specific position in the string.
Let’s take a closer look at how strings are created
String Literals
As we know, we can create a text string by surrounding with quotation marks:
string1 = 'Hello, World'
string2 = "1234"
Here we have two options, we can use single quotes (string1) or double quotes (string2)
to create a text string by putting quotation marks (") in the beginning and end of the text.
Whenever we create a string by surrounding text with quotation
marks, this string is called a string literal. The name shows that
the string is written out in your code. All the strings we have seen so far are string literals.
Python knows where a string begins and where it end by double quotation (are called delimiters). When one type of
quotes is used as the delimiter, then we use other type inside the
string:
string3 = "We're #1!"
string4 = 'I said, "you are not allowed to go there."'
After Python reads the first delimiter, it considers all the characters
after it part of the string until it reaches a second matching delimiter.
This is why we can use a single quote in a string delimited by double
quotes.
If we try to use double quotes inside a string delimited by another double
quotes, we will get an error:
text = "She said, "What time is it?""
File "", line 1
text = "She said, "What time is it?""
SyntaxError: invalid syntax
Python throws a Syntax error because it thinks the string ends after the
second ", and it doesn’t know how to interpret the remaining part of the line. If
we need to include a quotation mark that matches the delimiter inside a string, then we can escape the character using a backslash (\):
text = "She said, \"What time is it?\""
print(text)
She said, "What time is it?"
Strings can contain any valid Unicode character. For example, the
string "We're $1!" contains the dollar sign ($) and "12345" contains numbers. "×Pýŧħøŋ×" is also a valid Python string!
Length of a String
Total numbers of character including spaces in a string, is
called the length of that string. For example, the string "programming" has a
length of 11, and the string "python programming" has a length of 18.
Python has a built-in len() function that we can use to determine the
length of a string. in the below given code we can understand how len() works.
len("python programming")
Outpur: 18
letters = "python programming"
len(letters)
Output: 18
we can also use len() to get the length of a string that’s assigned to a
variable:
0 Comments