Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

stringbuilder python

# StringIO can be used to read/write strings from/to memory buffer. 

# import StringIO
try:
    from StringIO import StringIO # for Python 2
except ImportError:
    from io import StringIO # for Python 3

mylist = ['abcd' for i in range(5)]  # myList = [abcdabcdabcdabcdabcd]
file_str = StringIO()  		     # create StringIO object
for i in range(len(mylist)):
    file_str.write(mylist[i])	     # write data to StringIO object
print(file_str.getvalue())	     # abcdabcdabcdabcdabcd

"""
Alternatively, we can rely on the StringIO module
to create a class having same capabilities as
StringBuilder of other languages.
"""
class StringBuilder:
     _file_str = None

     def __init__(self):
         self._file_str = StringIO()

     def Append(self, str):
         self._file_str.write(str)

     def __str__(self):
         return self._file_str.getvalue()

sb = StringBuilder()

sb.Append("Hello ")
sb.Append("World")

print(sb)  # Hello World
Comment

PREVIOUS NEXT
Code Example
Python :: pandas groupby count occurrences 
Python :: python get lines from text file 
Python :: python import ndjson data 
Python :: python maths max value capped at x 
Python :: python find location of module 
Python :: python list methods 
Python :: how to find the multiples of a number in python 
Python :: python pandas convert comma separated number string to integer list 
Python :: mark_safe django 
Python :: deleting duplicates in list python 
Python :: python dedent 
Python :: python des 
Python :: python numpy arrays equal 
Python :: python print to stderr 
Python :: explode dictionary pandas 
Python :: select rows with nan pandas 
Python :: how to write a numpy array to a file in python 
Python :: libreoffice add line in table 
Python :: limpiar consola en python 
Python :: how to import iris dataset 
Python :: sort value_counts output 
Python :: simple jwt django 
Python :: python random choice int 
Python :: blackjack in python 
Python :: Iterate through python string starting at index 
Python :: python trick big numbers visualisation 
Python :: python datetime difference in seconds 
Python :: get last day of month python 
Python :: extract filename from path in python 
Python :: pathlib current directory 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =