![](https://static.isu.pub/fe/default-story-images/news.jpg?width=720&quality=85%2C50)
17 minute read
Syntax
by Mrexamples
string index(value, start, end)
Parameter Values end A choice. You can stop the search at this position – Strings are terminated at the end by default.
Advertisement
Parameter Summary value This is necessary A String. Look for the string value. start A choice. Begin the search at this position. It is set to 0 by default.
What is the location of the word “python” in the text? :
Index() Example:1 mrx = "Hey future developer!, welcome to the python string index() method." ample = mrx.index("python") print(ample)
What is the first appearance of “r” in the text? :
Index() Example:2 mrx = "Hey future developer!, welcome to the python string index() method." ample = mrx.index("r") print(ample)
If you only search between positions 20 and len(mrx), where is the first appearance of the character “r”? : mrx = "Hey future developer!, welcome to the python string index() method." ample = mrx.index("r", 20, len(mrx)) print(ample)
The find() method returns -1 if the value could not be located, but the index() method will throw an error: mrx = "Hey future developer!, welcome to the python string index() method." print(mrx.index("z")) #throws an exception print(mrx.find("z")) #returns -1
Capitalize the string first, utilize the count() method to the capitalized string then apply the index() method: mrx = "hey future developer!, welcome to the python string index() method." mrx = mrx.capitalize() print(mrx) print(mrx.count("H")) ample = mrx.index("h") print(ample)
Python Strings Method isalnum()
When all the characters are letters (a-z) and digits (0-9), then the isalnum() method produces True.
Non-alphanumeric characters: (space)!#%&? etc.
Parameters are not required – Make sure each of the characters in the string is alphanumeric: mrx = "Nimbus2000" ample = mrx.isalnum() print(ample)
Verify that each of the characters in the string is alphanumeric: mrx = "iPhone 14" ample = mrx.isalnum() print(ample)
Apply the underscore to the alphanumeric string: mrx = "iPhone_14" ample = mrx.isalnum() print(ample)
Python Strings Method isascii()
If each of the characters are ASCII characters, the isascii() method displays True.
Syntax string.isascii()
Parameters are not required – Make sure each of the characters in the string is ASCII: mrx = "Rain, snow, mud, and off-road conditions are no problem for the Model Y of Tesla." ample = mrx isascii() print(ample)
Verify that each character in the mrx string is ASCII:
Isascii() Example:2 mrx = "Python pronunciation: ˈ pīˌTHän" ample = mrx.isascii() print(ample)
Python Strings Method isdecimal()
If each of the characters is a decimal (0-9), the isdecimal() method outputs True.
Unicode objects utilize this method.
Syntax string isdecimal()
Parameters are not required – Verify that all the characters in the Unicode object are decimal: Isdecimal() Example:1 mrx = "\u0039" #unicode for 9 ample = mrx.isdecimal() print(ample)
Make sure that each of the characters in Unicode are decimals:
Isdecimal() Example:2 mrx = "\u0036" #unicode for 6 ample = "\u0041" #unicode for A print(mrx.isdecimal()) print(ample.isdecimal())
Ensure that each Unicode character is a decimal:
Isdecimal() Example:3 mrx1 = "\u0038" #unicode for 8 mrx2 = "\u0048" #unicode for H mrx3 = "\u005F" #unicode for underscore(_) print(mrx1.isdecimal()) print(mrx2.isdecimal()) print(mrx3 isdecimal())
Python Strings Method isdigit()
If each and every character is digits, the isdigit() method provides True, else False.
Numbers with exponents, such as ², are also regarded as digits.
Syntax string isdigit()
Parameters are not required – Verify that all of the string characters are numbers:
Isdigit() Example:1 mrx = "7800000" ample = mrx.isdigit() print(ample)
First, separate the thousand by an underscore, then verify that all string characters are digits: mrx = "10_000_000" ample = mrx.isdigit() print(ample)
Prove that all of the string characters are numbers: mrx = "\u0039" #unicode for 9 ample = "\u00B3" #unicode for ³ print(mrx.isdigit()) print(ample isdigit()) Python Strings Method isidentifier()
If the string is a correct identifier, isidentifier() provides True, else False.
Strings with only alphanumeric characters (a-z) and zero through nine, or underscores ( ), are accepted as correct identifiers.
A correct identifier cannot begin with a number or include any spaces.
Syntax string.isidentifier()
Parameters are not required – Verify that the string is a correct identifier:
= "mrexamples" ample = mrx.isidentifier() print(ample)
Make sure the string is the correct identifier: Isidentifier() Example:2 mrx = "isidentifier_Method" ample = mrx.isidentifier() print(ample)
To find out if the strings are correct identifiers, check the following: Isidentifier() Example:3 mrx1 = "mrx-1" mrx2 = "Mrx1" mrx3 = "1ample" mrx4 = "ample one" mrx5 = "Mrx_1" mrx6 = "@mple" print(mrx1.isidentifier()) print(mrx2.isidentifier()) print(mrx3.isidentifier()) print(mrx4.isidentifier()) print(mrx5.isidentifier()) print(mrx6.isidentifier())
Python Strings Method islower()
If all the letters are in small case, the islower() method outputs True, else outputs False.
Only alphabetic characters are validated, not numbers, symbols, or spaces.
Syntax string.islower()
There are no parameters – Make sure all the letters in the string are lowercase: mrx = "hey!! future developers, welcome to the python string islower() method." ample = mrx.islower() print(ample)
If the first character in a string is a digit, ensure that all the characters are in lower case: mrx = "1991 was the year when the first version of python was released." ample = mrx.islower() print(ample)
Verify that all the letters in the multiple strings are in smaller case: mrx1 = "mr_examples" mrx2 = "iphone 14" mrx3 = "Steve Jobs is the owner of the Apple company" mrx4 = "iphoneX" print(mrx1 islower()) print(mrx2.islower()) print(mrx3.islower()) print(mrx4.islower())
Python Strings Method isnumeric()
When all characters (0-9 are numeric), isnumeric() provides True, else it gives False.
Numerical values include exponents, such as ⁷ and ¾.
“-7” and “4.5” are unable to be accepted as numeric values, because every characters in the string have to be numeric, and the – and “.” are not.
Syntax
string.isnumeric()
Parameters are not required – Verify that all of the string characters are numeric: mrx = "2³" ample = mrx.isnumeric() print(ample)
First, separate the thousand by a comma in the string, then verify that all string characters are numeric: mrx = "48,000,000" ample = mrx.isnumeric() print(ample)
Make sure the characters are in numeric form: mrx1 = "\u0039" #unicode for 9 mrx2 = "\u00B3" #unicode for ³ mrx3 = "90mph" mrx4 = "3.67" mrx5 = "-7" mrx6 = "7¾" print(mrx1.isnumeric()) print(mrx2.isnumeric()) print(mrx3 isnumeric()) print(mrx4.isnumeric()) print(mrx5.isnumeric()) print(mrx6.isnumeric())
Python Strings Method isprintable()
Isprintable() returns True if all characters are printable, otherwise False.
In addition to carriage returns and line breaks, there are several non-printable characters.
Syntax string.isprintable()
Parameters are not required – Verify that all of the string characters are printable: Isprintable() Example:1 mrx = "Tesla latest model: Model Y" ample = mrx.isprintable() print(ample)
Make sure the string is printable by checking the following: Isprintable() Example:2 mrx = "Tesla latest model:\tModel Y" ample = mrx.isprintable() print(ample)
Ensure that all the characters in the string are printable:
Isprintable() Example:3 mrx = "#Tesla latest model:\nModel Y" ample = mrx.isprintable() print(ample)
Python Strings Method isspace()
If all the characters in a string are empty spaces, the isspace() method returns True, while it returns False.
Syntax string isspace()
Parameters are not required – Make sure all the characters in the string are empty spaces: ample = mrx.isspace() print(ample)
Verify that all the characters in the string are blank spaces:
Isspace() Example:2 mrx = " MRX " ample = mrx.isspace() print(ample)
Find out that all the characters in the string are empty spaces:
Isspace() Example:3 mrx = "Mr examples" ample = mrx.isspace() print(ample)
Python Strings Method istitle()
When all words in a text have an uppercase letter and the remainder are lowercase letters, the istitle() method provides True; else, it provides False.
It is not necessary to include symbols or numbers.
Syntax string.istitle()
Parameters are not required – Make sure every word begins with an uppercase letter: mrx = "Guido Van Rossum Is The Founder Of Python Language" ample = mrx.istitle() print(ample)
Verify that every word begins with an uppercase letter in the following example: mrx = "Guido Van Rossum Is The Founder Of PYTHON Language" ample = mrx.istitle() print(ample)
In the following example, ensure that each word in the string begins with an uppercase letter: Example:
Python Strings Method isupper()
If all the letters are in larger case, the isupper() method outputs True, else outputs False.
Only alphabetic characters are validated, not numbers, symbols, or spaces.
Syntax string.isupper()
There are no parameters – Make sure all the letters in the string are uppercase:
Isupper() Example:1 mrx = "HELLO PYTHON DEVELOPERS!!" ample = mrx.isupper() print(ample)
If the first character in a string is a digit, ensure that all the characters are in upper case: mrx = "1991 WAS THE YEAR WHEN THE FIRST VERSION OF PYTHON WAS RELEASED." ample = mrx.isupper() print(ample)
Verify that all the letters in the multiple strings are in larger case: mrx1 = "MR_EXAMPLES" mrx2 = "IPHONE 14" mrx3 = "Steve Jobs is the owner of the Apple company" mrx4 = "iPHONE 14 PRO MAX" print(mrx1.isupper()) print(mrx2 isupper()) print(mrx3.isupper()) print(mrx4.isupper())
Python Strings Method join()
In the join() method, each of the elements in an iterable is joined into a single string. As a separator, you have to provide a string.
Syntax string.join(iterable)
Parameter Values
Parameter Summary iterable
It is necessary. If all the retrieved items are strings, then the iterable object is String.
By applying a ” –> ” as a separator, join all elements in a tuple into a string:
Join() Example:1 digit_tuple = ("1", "2", "3", "4", "5") mrx = " --> ".join(digit_tuple) print(mrx)
Utilize a “\n” as a separator, join all elements in a tuple into a string:
Join() Example:2 digit_tuple = ("1", "2", "3", "4", "5") mrx = "\n".join(digit_tuple) print(mrx)
Create a string by connecting all entries in a dictionary with the word “TRY” as a separating word: bio_dict = {"name": "Harry", "age":19, "profession": "Football", "country": "United Sates of America"} separate_by = "TRY" mrx = separate_by.join(bio_dict) print(mrx)
Reminder: As an iterable, a dictionary displays the keys, not the values.
Python Strings Method ljust()
By calling the ljust() method, a chosen character (space by default) will be assigned as the fill character of the string.
Syntax string.ljust(length, character)
Parameter Values character A choice. To occupy the blank space (to the right of the string), enter a character. The default value is ” ” (space).
Parameter Summary length This is necessary Provides the length of the string.
By applying a ” –> ” as a separator, join all elements in a tuple into a string: ljust() Example:1 mrx = "Python" ample = mrx.ljust(35) print(ample, "is the easiest programming language.")
Utilize a “\n” as a separator, join all elements in a tuple into a string: ljust() Example:2 mrx = "PYTHON" ample = mrx.ljust(50, "*") print(ample)
Create a string by connecting all entries in a dictionary with the word “TRY” as a separating word: ljust() Example:3 bio_dict = {"name": "Harry", "age":19, "profession": "Football", "country": "United Sates of America"} separate_by = "TRY" mrx = separate_by.join(bio_dict) print(mrx)
Python Strings Method lower()
lower() method generates a string that contains all letters in small case.
Numbers and symbols are not taken into account.
Syntax string lower()
There are no parameters – Make the string smaller case: mrx = "Welcome to PYTHON Strings lower() Method." ample = mrx.lower() print(ample)
Apply the digits at the beginning of the string then convert the string to small case: mrx = "3.11.1 is the latest version of PYTHON!" ample = mrx.lower() print(ample)
Python Strings Method lstrip()
lstrip() eliminates any preceding characters (the space character is the default preceding character).
Syntax string.ljust(length, character)
Parameter Values
Parameter Description characters A choice. To eliminate as main characters a set of characters.
Left-justify the string by eliminating spaces:
Example: mrx = " BEST>>>>>>>>>" ample = mrx.lstrip() print("Python is the", ample,"Programming Language")
Pass the value “>” in the lstrip() method:
Example: mrx = " BEST>>>>>>>>>" ample = mrx.lstrip(">") print("Python is the", ample,"Programming Language")
Take out the preceding characters: mrx = "-->-->-->-->-->-->BEST>>>>>>>>>" ample = mrx.lstrip("->") print("Python is the", ample,"Programming Language")
Python Strings Method maketrans()
Maketrans() method provides a mapping table that can be utilized with the translate() method to substitute selected characters.
Syntax
string.maketrans(x, y, z)
Parameter Values
Parameter Description x This is necessary. A dictionary explaining how to execute the substitution must be given if only one parameter is given. A string indicating the characters to substitute must be given if two or more parameters are given. y A choice. The length of the string is the same as the parameter x. In this string, every character in the initial parameter will be substituted with its equivalent character z A choice. Indicates which characters must be deleted from the original string.
Utilize the mapping table in the translate() method to substitute any “G” characters for “H” characters: mrx = "Garry: We are learning python string maketrans() methods" ample = mrx.maketrans("G", "H") print(ample) #showing decimal number of ASCII characters G and H print(mrx.translate(ample))
Initially implement the lower() method on string, then apply the maketrans() method: mrx = "KANE: We are learning Python String Methods" mrx = mrx.lower() ample = mrx.maketrans("k", "j") print(mrx.translate(ample))
To substitute multiple characters, utilize a mapping table: Example: mrx = "Eva" ample1 = "aEv" ample2 = "yAm" mrx_table = mrx.maketrans(ample1, ample2) print(mrx.translate(mrx_table))
You can delete characters from the string through the third parameter in the mapping table:
Example: mrx = "Bonjour Eva!!" ample1 = "Eva" ample2 = "Lia" ample3 = "Bonjour" mrx_table = mrx.maketrans(ample1, ample2, ample3) print(mrx.translate(mrx_table))
In Unicode, the maketrans() method generates a dictionary explaining each substitution: mrx = "Bonjour Eva!!" ample1 = "Eva" ample2 = "Lia" ample3 = "Bonjour" print(mrx maketrans(ample1, ample2, ample3))
Python Strings Method partition()
Through the partition() method, you can look for a string and divide it into tuples with three elements.
In the first element, the part preceding the given string is included.
The string mentioned in the second element can be found in the second element. After the string, the third element includes the part.
Reminder: This method finds the initial appearance of the given string.
Syntax string.partition(value)
Parameter Values
Parameter Description value
It is necessary. Specify the search string
Provide a tuple with three elements that include the word “partition()“:
1. Anything that precedes “equivalent”
2. The “equivalent”
3. Anything that follows “equivalent”
Example: mrx = "Python Strings partition() Method" ample = mrx.partition(" partition() ") print(ample)
Apply the title() method to the mrx string, then utilize the partition() method: mrx = "python strings method" mrx = mrx.title() ample = mrx.partition("Strings") print(ample)
Partition() method generates a tuple with: first the whole string, second an unfilled string, third an unfilled string if the given value is not present: mrx = "Python Strings partition() Method" ample = mrx partition("maketrans()") print(ample)
Python Strings Method replace()
By calling the replace() method, you can substitute a phrase for a new phrase.
Reminder: A substitution will be executed on all occurrences of the given phrase if nothing else is provided.
Syntax string.replace(oldvalue, newvalue, count)
Parameter Values
Parameter Description oldvalue It is necessary. Specify the search string newvalue This is necessary. Replacement string for the previous value count A choice. The number of instances of the previous value you want to replace. All instances are set to default.
Substitute “3.8.0” to “3.11.1”:
Example: mrx = "Latest version of Python is 3.8.0" ample = mrx.replace("3.8.0", "3.11.1") print(ample)
Utilize the replace() method on a string, then implement the partition() method: Example: txt = "Black…White…Blue" x = txt.replace("…", "") ample = x.partition("White") print(ample)
Change all appearances of “two” to “nine”: Example: mrx = "The net worth of Bill Gates is one hundred two billion dollars, and Jeff Bezos is also one hundred two billion dollars." ample = mrx.replace("two", "nine") print(ample)
Change the first appearance of the word “two” to “nine”: Example: mrx = "The net worth of Bill Gates is one hundred two billion dollars, and Jeff Bezos is also one hundred two billion dollars." ample = mrx.replace("two", "nine", 1) print(ample)
Python Strings Method rfind()
rfind() method locates the last appearance of a value.
A value cannot be located if the rfind() method returns -1.
The rfind() method is nearly identical to the rindex() method. You can see an example below.
Syntax
string.rfind(value, start, end)
Parameter Values
Parameter Description value
It is necessary. Specify the search string. start A choice. Searching for where to begin. It is set to 0 by default. end A choice. End the search here. As default, it ends at the last.
In what part of the text does the string “language” occur at the end? : Example: mrx = "Python is an easy to learn language but it is also a vast language" ample = mrx.rfind("language") print(ample)
Utilize the count() method on a string, then apply the rfind() method: mrx = "Python is an easy to learn language but it is also a vast language" print(mrx.count("language")) ample = mrx.rfind("language") print(ample)
In the string where is the last appearance of the character “o”? : mrx = "Greetings, we are learning about the Python rfind() method" ample = mrx.rfind("o") print(ample)
When you only find between positions 0 and 36, where is the final appearance of the character “t”? : mrx = "Greetings, we are learning about the Python rfind() method" ample = mrx.rfind("t", 0, 36) print(ample)
The rfind() method provides -1 if the value cannot be located, but the rindex() method throws an error if the value cannot be found: mrx = "Greetings, we are learning about the Python rfind() method" print(mrx.rfind("x")) print(mrx.rindex("x"))
Python Strings Method rindex()
The rindex() method searches for the last instance of a given value. If the value is not found, an error will occur.
It is essentially identical to the rfind() method- please refer to the example for more information.
Syntax string.rindex(value, start, end)
Parameter Values
Parameter Description value
It is necessary. Specify the search string. start A choice. Searching for where to begin. It is set to 0 by default. end A choice. End the search here. As default, it ends at the last.
In what part of the text does the string “language” occur at the end? : Example: mrx = "Python is an easy to learn language but it is also a vast language" ample = mrx.rindex("language") print(ample)
Utilize the count() method on a string, then apply the rindex() method: mrx = "Python is an easy to learn language but it is also a vast language" print(mrx.count("language")) ample = mrx.rindex("language") print(ample)
In the string where is the last appearance of the character “o”? : mrx = "Greetings, we are learning about the Python rfind() method" ample = mrx.rfind("o") print(ample)
When you only find between positions 0 and 36, where is the final appearance of the character “t”? : mrx = "Greetings, we are learning about the Python rindex() method" ample = mrx.rindex("t", 0, 36) print(ample)
The rfind() method provides -1 if the value cannot be located, but the rindex() method throws an error if the value cannot be found: mrx = "Greetings, we are learning about the Python rindex() method" print(mrx.rindex("x")) print(mrx.rfind("x"))
Python Strings Method rjust()
The rjust() method aligns text to the right side of a string, utilizing either a space or a given character as padding.
Syntax
string rjust(length, character)
Parameter Values
Parameter Description length This is necessary. Provides the length of the string character A choice. To occupy the blank space (to the left of the string), enter a character. The default value is ” ” (space).
Provide a right-aligned 35-character representation of “Python”:
= "Python" ample = mrx.rjust(35) print(ample, "is the easiest programming language.")
As a padding character, utilize the symbol “*”: mrx = "PYTHON" ample = mrx.rjust(50, "*") print(ample)
For a padding character, apply the symbols “>” and “<“:
Example: 1
2 3 4 5 6 mrx1 = "String " mrx2 = "rjust() " ample1 = mrx1.rjust(30, ">") ample2 = mrx2.rjust(30, "
Python Strings Method rpartition()
The rpartition() method looks for the final instance of a given string, which it then divides into a tuple of three elements: The part before the string, the string itself, and the part after it.
Syntax string.rpartition(value)
Parameter Values
Parameter Description value It is necessary. Specify the search string.
Provide a tuple with three elements that include the word “rpartition()“:
1 – Anything that precedes “equivalent”
2 – the “equivalent”
3 – Anything that follows “equivalent” mrx = "Programming language Python has the rpartition() method. In Python it's a builtin method" ample = mrx.rpartition("Python") print(ample)
Apply the title() method to the mrx string, then utilize the rpartition() method: mrx = "programming language python has the rpartition() method. In python it's a builtin method" mrx = mrx.title() ample = mrx.rpartition("Python") print(ample)
The rpartition() method generates a tuple with: first an unfilled string, second an unfilled string if the given value is not present, third the whole string: mrx = "programming language python has the rpartition() method. In python it's a builtin method" ample = mrx.rpartition("rfind()") print(ample)
Python Strings Method rsplit()
The rsplit() method will separate a string into a list beginning from the right, unless the “max” parameter is set. It results in the same outcome as the split() function.
Reminder: When maxsplit is given, the resulting list can include up to that many elements plus one.
Syntax
string rsplit(separator, maxsplit)
Parameter Values
Parameter Description separator A separator can be chosen (optional), and by default, any whitespace will be used for splitting the string. maxsplit The number of splits is optional. The default value is -1, meaning all instances will be split.
Divide a string into a list by utilizing a comma and a space (“, “) as the delimiter.
Example: mrx = "Python, Java, C, C++, C#, PHP" ample = mrx rsplit(", ") print(ample)
Apply the rsplit() method to the mrx string, then utilize the join() method: Example: mrx = "Python, Java, C, C++, C#, PHP" ample = mrx.rsplit(", ") ample1 = "–".join(ample) print(ample1)
Divide the string into a list of at most 4 items:
Example: mrx = "Python, Java, C, C++, C#, PHP" ample = mrx.rsplit(", ", 3) print(ample)
# By setting the maxsplit parameter to 3, you will get a list of 4 elements!
# The output has four values. 'Python, Java, C' is the first, 'C++' is the second, 'C#' is the third, 'PHP' is the last.
Python Strings Method rstrip()
With the rstrip() method, you can eliminate trailing characters (the last characters in a string).
By default, the following character is a space.
Syntax string rstrip(characters)
Parameter Values
Parameter Description characters A choice. To eliminate leading characters from a set of characters.
At the last of the string, delete any empty spaces: mrx = "**********BEST " ample = mrx.rstrip() print("Python is the", ample,"Programming Language")
Pass the value “*” in the rstrip() method: mrx = "**********BEST " ample = mrx.rstrip("*") print("Python is the", ample,"Programming Language")
Take out the following characters: mrx = "-->-->-->-->-->-->BEST>>>>>>>>>" ample = mrx.rstrip(">") print("Python is the", ample,"Programming Language")
Python Strings Method split()
Strings can be divided into lists utilizing the split() method. Default separator is any empty space, but you can choose it.
Reminder: Selecting maxsplit will result in a list with the given number of elements plus one.
Syntax string split(separator, maxsplit)
Parameter Values
Parameter Description separator A separator can be chosen (optional), and by default, any whitespace will be used for splitting the string. maxsplit The number of splits is optional. The default value is -1, meaning all instances will be split.
Make a list from a string, with each word appearing as an item: mrx = "We are learning Python strings split() method" ample = mrx.split() print(ample)
Divide a string into a list by utilizing a comma and a space (“, “) as the delimiter. Example: mrx = "Python, Java, C, C++, C#, PHP" ample = mrx.split(", ") print(ample)
Apply the split() method to the mrx string, then utilize the join() method: Example: mrx = "Python, Java, C, C++, C#, PHP" ample = mrx.split(", ") ample1 = "***".join(ample) print(ample1)
In place of a separator, utilize the dollar “$” character: mrx = "Python$Java$C$C++$C#$PHP" ample = mrx.split("$") print(ample)
Divide the string into a list of at most 4 items: mrx = "Python, Java, C, C++, C#, PHP" ample = mrx.split(", ", 3) print(ample)
# By setting the maxsplit parameter to 3, you will get a list of 4 elements!
# The output has four values. 'Python' is the first, 'Java' is the second, 'C' is the third, 'C++, C#, PHP' is the last.
Python Strings Method splitlines()
A string is divided into a list utilizing the splitlines() method. Line breaks are used to divide the text.
Syntax string.splitlines(keeplinebreaks)
Parameter Values
Parameter Description keeplinebreaks A choice. Indicates whether line breaks should be inserted (True), or not (False). False is the default value.
Divide a string into a list with each line representing an item: Example: mrx = "programming language python has the splitlines() method.\nIn python it's a builtin method" ample = mrx.splitlines() print(ample)
Make a list where each line is a list element from a string: mrx = "**\nprogramming language python has the splitlines() method.\tIn python it's a built-in method\n**" ample = mrx.splitlines() print(ample)
Maintain the line breaks when dividing the string: mrx = "programming language python has the splitlines() method.\nIn python it's a builtin method" ample = mrx.splitlines(True) print(ample)