The assembly of a pilus, a short filamentous structure on a bacterial cell, flagella-like in structure and generally present in many copies. Pili are variously involved in transfer of nucleic acids, adherence to surfaces, and formation of pellicles. Is required for bacterial conjugation, or can play a role in adherence to surfaces (when it is called a fimbrium), and in the formation of pellicles.