6 Interesting Facts about Python Strings


Facts about Python Strings including Concatenation which People don't know

Strings are a data-type programmers use almost every time they write a program, irrespective of size. Most people think they know everything there is to know about strings and its functions. Here are 6 things about Python strings that might surprise you !

<Note>You might know some or maybe even all of these facts. If yes, congratulations, you are a Strings Champion. Here is a medal for you🥇</Note>


#1. Strings are immutable:

Strings, unlike lists and dictionaries are immutable. Once a string has been defined, you cannot change the string. Any change to the string will be stored in a different memory space. 

#2. Strings are iterable:

Strings may be immutable, but like lists, they are iterable. You can put a list in a for-loop and each iteration of the for-loop will be for one character from the string. This code snippet will give us a better example: 

concatenate strings format

This snippet will give us every letter of Hello printed one after the other, proving that strings are, of course, iterable. 

#3. Strings are case sensitive: 

A very important thing to note about strings is that they are case sensitive, that is, case matters. Upper case letters and Lower case letters are not the same. Hello and hello are not equal (note the H and h ). 
Let's check this out with this code snippet: 

python regex strings concatenation equal

This code checks whether 'Hello' and hello are equal and will print True if they are and False if they are not. When we execute this...

python split format strings python

Just as we predicted, Python returns False, proving that according to Python, Upper-case version and Lower-case version of the same letter are different characters. 

#4. Strings can be added to each other and multiplied with a number: 

Yes, you read that right. You can add two strings. This process is called concatenation. Just put a '+' between two strings and that's it, you have a combination of the two strings. Let's check it out with an example code snip: 


and this gives us: 




Which is a combination of Hello and hello, our two strings. But, the fun thing is... strings can be multiplied with a number. When you multiply a string and a number, you get a combination of the same string that number of times. Like this: 




This gives us...




#5. Strings can be in more than one line:

This is a very useful fact when you need to work with a paragraph or a bunch of paragraphs. Instead of the usual single pair of quotes (' ' or " "), the string is written in between three pairs of quotes (''' ''' or """ """). Here is an example: 



When we execute this, we get a multiple-line string that looks like this: 


Which is just like how we wanted it to be. Cool, right ?

#6. Slashes allow you to enter special character in strings: 

Many people wonder how to enter a quote in a string without breaking the string, or how to type an escape character but tell the computer not to take it as a special character. You may think it is very tough, but actually it may be the simplest point in this post. All you have to do is add a backward slash ('\') before that specific character or combination. For example, let us assume you want to print the string 'Python says "Hello". This string has quotes which will break the string definition. So, just add a backward slash before the interior quotes and you're done. 


 
This will print...



Which is exactly what we wanted...

Bonus. Reversing a String:

It may seem tough, but Python offers a trick to reverse a string in one line of code. Given a string (Let's call it String1), all you need to do to reverse it is write this line of code: 


Just adding [::-1] to the end of the string variable, reverses it. It's a cool trick and very useful when you need to reverse a string or check for a palindrome condition. 


So there it is, here are 6 (+1) things you may not know about Strings in Python. If you found it useful, do follow ThePygrammer so you will be the first to know whenever such useful posts come out. Do use the comment section below to share your comments and queries. 

If you want to learn more about the other data-types in Python: Data-Types in Python.
If you want to know how to read and write on text-files with Python: Text-Files with Python










  


Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment