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:
1 2 |
print('Hello World') Hello World |
Or double quotes:
1 2 |
print("I learn Python") I learn Python |
And three times of double quotes:
1 2 |
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:
1 2 3 4 5 |
“Python Learnings’ 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:
1 2 3 4 5 6 7 8 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
A="Hello" type(A) Out[14]: str print(A) Hello howdy="Fine and you?" print(howdy) Fine and you? #OR JUST TYPE THE VARIABLE NAME howdy Out[19]: '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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
a="john" a[0] Out[21]: 'j' a[2] Out[22]: 'h' a[:] Out[23]: 'john' a[-1] Out[24]: 'n' a[-3] Out[27]: 'o' # HERE : REPRESENTS START INDEX FROM THE BEGINING AND -1 IS "DO NOT INCLUDE LAST # CHARACTER" a[:-1] Out[25]: 'joh' |
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]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
b="Python Programming Language" # Means start from 4th index and go to 9. 10 is not included. b[4:10] Out[29]: 'on Pro' # From the beginning to 9. Spaces are also counted as one character. b[:10] Out[31]: 'Python Pro' # From the 4th position to the end. b[4:] Out[32]: 'on Programming Language' # Start from 4th position to 12th skipping by 3 characters. b[4:12:3] Out[33]: 'oPg' |
SOME STRING PROPERTIES
- To see length of a string, we use len() function.
1 2 3 4 |
len(b) Out[34]: 27 # b variable is composed by 27 chars. |
- You can not modify directly a character of a string.
1 2 3 4 5 6 7 8 9 |
b[0] Out[35]: 'P' b[0]='T' 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> b[0]='T' 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
a="python" b="programing" c="language" a+b+c Out[40]: 'pythonprograminglanguage' # USE " " AS SPACE IF YOU WANT TO LEAVE SPACES BETWEEN WORDS. a+" "+b+" "+c Out[44]: 'python programing language' d="big" d + " " + "red glass" Out[47]: 'big red glass' |
- Multiplication is allowed for strings
1 2 3 4 5 |
z="python" z*3 Out[52]: 'pythonpythonpython' # Prints Python three times in a row. |
Leave A Comment