LoveEnigma
0
Q:

python import stringIO

try:
    from StringIO import StringIO ## for Python 2
except ImportError:
    from io import StringIO ## for Python 3
0
# Importing the StringIO module. 
from io import StringIO   
  
  
# The arbitrary string. 
string ='This is initial string.'
  
# Using the StringIO method to set 
# as file object. Now we have an  
# object file that we will able to 
# treat just like a file. 
f = StringIO(string) 
  
# this will read the file  
print(f.read()) 
  
# We can also write this file. 
file.write(" Welcome to geeksforgeeks.") 
  
# This will make the cursor at  
# index 0. 
f.seek(0) 
  
# This will print the file after  
# writing in the initial string. 
print('The string after writing is:', file.read())  
0

New to Communities?

Join the community