How to use String in Python
- String Data Type is used to store or collect one or more characters or sequence of characters, we can place any alphanumerical or special character in a string.
- Let's create a string in python, it has a simple syntax, as shown below:
first_var = "Hello World"
- There are two sorts of strings, we can use in python:
- Single Line.
- Multiple Lines.
- The above-given example is for a single line, like if you want to write an email or product name, etc.
- If you want to write an essay, story, report etc. then you will need to use Multiple Lines string, which is created by placing triple quote around the data, as shown in below figure:
- As you can see in above figure, we have written multiple lines in welcome string.
Strings Operators
- If you are using an apostrophe, you will need to use use double quotes, otherwise, the interpreter will not be able to understand it and will give you a syntax error, as shown in below figure:
- But if I write in double-quotes, then it will work fine, as shown in below figure:
Escape sequences in Python
- If you are using double quotes in the same string, then you will need to use a backward slash ( \ ), as shown in the image.
- Run the program and see the result in the console window:
- If I want to add a new line break then I will use escape sequence ( \n ).
- As you can see in below figure, I have printed the name & age of Richard in separate lines using escape sequence ( \n ).
- I wrote it with tab escape sequence ( \t ) and run the program, see the six spaces in the printed window:
- You can use backward slash and forward slash \/ like this.
- You cannot use the backward slash at the end of the string before the end of the quote.
- You will use double backward slash ( \\ ), if you want to print one.
Concatenation in Python
- In concatenation, we connect multiple strings together, we use ( + ) sign in order to concatenate strings.
- Let's understand it with an example, as shown in below figure:
- As you can see in above figure, I have printed multiple strings in a single line using ( + ) sign.
String Formatting In Python
When we use multiple strings, it gets harder to concatenate those strings, and it is difficult to remember each string format and codes. In such cases, we need to format those strings. Let's understand it with an example:- In String Formatting, we simply place our variables in curly brackets, as shown in below figure:
- No need to add multiple quotes and + symbol, instead simply use curly brackets for variables.
String Indexes in Python
In python, when we store a string, it goes with an index for each of its element one by one, because it is a sequence of characters. Let's understand it with an example:- For example, I saved the name "Ali Haider" in a string then each of its character has an index assigned with it.
- I have shown the string and the index starting with a comment # in below image:
- So, that means index of A=0, L=1, I=2, (for blank space, it is also a character and its index is 3), H=4, a=5, i=6, d=7, e=8 and r=9.
- After that, I have printed first three characters of that string by writing a range of [0:3], it eliminates the ending index and shows the result as from 0 to 2, which makes the word Ali. (see above image )
- If you want to print till the final value, then you wont need to write the last index, you will just write it as [0: ].
- If I write in this way, print(player_name[:]), then it will print the whole string again.
- You can also write negative indexes like print(player_name[-1]) and it will print r from the right side.
- Type print("a" * 30) and check the magic in print window:
Builtin String Functions in Python
Python has numerous excellent builtin string functions, which we can access using DOT ( . ) operator. These builtin functions are quite helpful, so let's have a loot at few of them: string.upper()- This upper() function will make characters of the string uppercase, as shown in below figure:
- If, I write print(Precaution.isupper()), It will check the string, whether its uppercase or not.
- If string will be in uppercase it will return True and if it's not in uppercase, it will return False.
- Now let's convert string characters to lowercase by using lower() function.
- When I type print(precaution.lower()), It will print the whole string in lowercase, as shown in below figure:
- Now if we want to replace any word, then we need to use print(precaution.replace("WEAR", 'BUY')), It will replace the WEAR word with BUY, as shown in the image: