How to make string on Print Python

This article explains how to use the "n" escape character to create a new line in Python print statements, with an example.

Creating Strings in Python

Strings are a useful and important data type in Python. They are used to represent text-based data, and consist of a sequence of characters. Python strings are immutable, meaning they cannot be changed, only replaced with a new string. To create a string in Python, you must enclose the sequence of characters in single or double quotation marks.

For example, the string 'Hello World!' can be created by typing the following:

my_string = 'Hello World!'

You can access individual characters in a string by using the indexing operator []. Using the same example, we can access the letter “o” in “Hello World!” by typing the following:


This will return the character “o”. It is important to note that indexing starts at 0, meaning the first character in the string is at index 0, the second character is at index 1, and so on.

You can also use negative indexing, which starts at the end of the string and works backwards. For example, the last character in the string is at index -1, the second to last character is at index -2, and so on. To access the last character in the string, you would type the following:


This will return the character “!”. Negative indexing is useful for accessing characters at the end of a string without having to know the exact length of the string.

You can also use the slicing operator [:] to access a substring. This can be used to access a range of characters in a string. For example, to access the characters “Hello” from our string, we can type the following:


This will return the string “Hello”. It is important to note that the number 5 is not included in the substring, meaning that the substring will include characters 0-4.

You can use the len() function to find the length of a string. This can be useful when you want to access the last character in a string but don’t know the exact length. For example, if you wanted to access the last character in the string “Hello World!”, you could type the following:


This will return the character “!”. It is important to note that you must subtract 1 from the length of the string, as the length of the string does not include the last character.

Python also has a number of built-in functions for manipulating strings. For example, the strip() function can be used to remove leading and trailing whitespace from a string. The replace() function can be used to replace a substring with another substring. And the split() function can be used to split a string into a list of substrings.

Creating strings in Python is a fairly simple process, and these are just a few of the many tools available for manipulating strings in Python. With the right knowledge and a bit of practice, you’ll be creating and manipulating strings in no time!

Answers (0)