Print list without brackets in a single row

前端 未结 12 1427
北恋
北恋 2020-11-22 17:01

I have a list in Python e.g.

names = [\"Sam\", \"Peter\", \"James\", \"Julian\", \"Ann\"]

I want to print the array in a single line withou

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 17:39

    For array of integer type, we need to change it to string type first and than use join function to get clean output without brackets.

        arr = [1, 2, 3, 4, 5]    
        print(', '.join(map(str, arr)))
    

    OUTPUT - 1, 2, 3, 4, 5

    For array of string type, we need to use join function directly to get clean output without brackets.

        arr = ["Ram", "Mohan", "Shyam", "Dilip", "Sohan"]
        print(', '.join(arr)
    

    OUTPUT - Ram, Mohan, Shyam, Dilip, Sohan

提交回复
热议问题