In this article we will discuss different ways to create long multi line string in python.
Using triple quotes to create string of multiple lines
We can wrap the multi line string in triple quotes i.e. either <> and assign it to a string variable. It will be stored in same multi line format.
Checkout this example,
# Create string object from multiple lines strObj = '''Hello this is a very long string indeed very long.''' print(strObj)
Output:
Hello this is a very long string indeed very long.
Create single line string object from multiple lines
Using Brackets:
If we want to create a string object from long multiple lines but all should be stored in a single line then we should use brackets i.e.
# Create single line string object from multiple lines strObj = ("Hello this is a " "very long string " "indeed very long.") print(strObj)
Output:
Hello this is a very long string indeed very long.
Here all the given multiple lines are merged to a single line and assigned back to string variable.
Using Escape symbol:
We can create a single line string object from long multiple lines using escape character too i.e.
# Create single line string object from multiple lines strObj = "Hello this is a " \ "very long string " \ "indeed very long." print(strObj)
Output:
Hello this is a very long string indeed very long.
Here all the given multiple lines are merged to a single line and assigned back to string variable.
Using join() :
We can create a single line string object by joining multiple lines too i.e.
# Create single line string object from multiple lines strObj = ''.join(( "Hello this is a " "very long string " "indeed very long." )) print(strObj)
Output:
Hello this is a very long string indeed very long.
Here all the given multiple lines are merged to a single line and assigned back to string variable.
Complete example is as follows:
def main(): # Create string object from multiple lines strObj = '''Hello this is a very long string indeed very long.''' print(strObj) # Create single line string object from multiple lines strObj = ("Hello this is a " "very long string " "indeed very long.") print(strObj) # Create single line string object from multiple lines strObj = "Hello this is a " \ "very long string " \ "indeed very long." print(strObj) # Create single line string object from multiple lines strObj = ''.join(( "Hello this is a " "very long string " "indeed very long." )) print(strObj) if __name__ == '__main__': main()
Output:
Hello this is a very long string indeed very long. Hello this is a very long string indeed very long. Hello this is a very long string indeed very long. Hello this is a very long string indeed very long.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.