Strings are basically set of characters which can be enclosed by single or double quotes, like “hello”, “abc123” etc. If you ever need to deal with textual data, you’ll almost certainly need to work with it as a string object or a series of string objects. The string type, str, is a powerful, flexible means for manipulating string data.
You can create strings by single quote:
Or double quotes:
print("I learn Python")
I learn Python
And three times of double quotes:
print("""I am writing a word.""")
I am writing a word.
You must close your string with same type of quote that you put at the beginning. This will give the following error:
File "<ipython-input-4-52c28270f133>", line 2
" Python Learnings’'
SyntaxError: EOL while scanning string literal
You will get the same error if you try to print My father’s car. You should put this string into double quotes, like:
print('My father's car')
File "<ipython-input-10-8adc1ba3a19a>", line 1
print('My father's car')
SyntaxError: invalid syntax
print("My father's car")
My father's car
Other important thing is you can define a variable from string type then check the type of variable with type() command. Type will be str which means string for words and int / float for numbers. We will see numbers and basic mathematics operations later.
howdy="Fine and you?"
Fine and you?
#OR JUST TYPE THE VARIABLE NAME
Out: 'Fine and you?'
Here, as you see you do not have to put between quotes your variable. It’s interpreted automatically.
As strings formed from set of characters, they have their own position within a string. In “John” string; the place of j-o-h-n is called index. Index’s first position is always starts from 0 (zero). You can print an index from the beginning or end of a string. If you want to see last “n” character of “john” string type a[-1], to see all characters in the string use : sign,ex: a[:]
Look at this example:
# HERE : REPRESENTS START INDEX FROM THE BEGINING AND -1 IS "DO NOT INCLUDE LAST
Taking a part of a string is also possible. You can define from where you want to take this part, till which character and omission value.
General syntax is
[start index:end index:omission]
b="Python Programming Language"
# Means start from 4th index and go to 9. 10 is not included.
Out: 'on Pro'
# From the beginning to 9. Spaces are also counted as one character.
Out: 'Python Pro'
# From the 4th position to the end.
Out: 'on Programming Language'
# Start from 4th position to 12th skipping by 3 characters.
SOME STRING PROPERTIES
- To see length of a string, we use len() function.
# b variable is composed by 27 chars.
- You can not modify directly a character of a string.
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-36-f9a8163cbab6>", line 1, in <module>
TypeError: 'str' object does not support item assignment
- However, you can addition strings or append a new word into a string variable. Below, we will define a new variable.
# USE " " AS SPACE IF YOU WANT TO LEAVE SPACES BETWEEN WORDS.
a+" "+b+" "+c
Out: 'python programing language'
d + " " + "red glass"
Out: 'big red glass'
- Multiplication is allowed for strings
# Prints Python three times in a row.