How to put similar named objects into collection in python?
I would do the same in python that I could do in the UI, which is to use Select Pattern, and then move these selected items to a Collection.
In python:
bpy.ops.object.select_all( action='DESELECT' ) # Deselect all objects
bpy.ops.object.select_pattern(pattern=&quot;Sphere*&quot;)

# if collection Sphere does not yet exist:
bpy.ops.object.move_to_collection( collection_index=0, is_new=True, new_collection_name='Sphere' ) # new collection
# else if collection Sphere does exist:
bpy.ops.object.move_to_collection( collection_index=2 ) # need collection index