Làm thế nào để bạn tạo một khoảng trống trong python?

Python – Thêm khoảng trắng giữa các từ tiềm năng

Cải thiện bài viết

Lưu bài viết

Thích bài viết

  • Cập nhật lần cuối. 06 tháng 10 năm 2022

  • Đọc
  • Bàn luận
  • khóa học
  • Thực tiễn
  • Video
  • Cải thiện bài viết

    Lưu bài viết

    Danh sách Chuỗi đã cho, nhiệm vụ là thêm khoảng trắng trước chuỗi bắt đầu bằng chữ in hoa

    Đầu vào. test_list = [“gfgBest”, “forGeeks”, “andComputerScienceStudents”] 
    Đầu ra. [‘gfg Best’, ‘for Geeks’, ‘and Computer Science Students’] 
    Giải thích. Các từ được phân tách bằng chữ hoa.

    Đầu vào. test_list = [“ComputerScienceStudentsLoveGfg”] 
    Đầu ra. [‘Sinh viên khoa học máy tính yêu thích Gfg’] 
    Giải thích. Các từ được phân tách bằng chữ hoa.
     

    Phương pháp số 1. Sử dụng vòng lặp + tham gia[]

    Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong phần này, chúng tôi thực hiện nhiệm vụ lặp lại tất cả các chuỗi và sau đó là tất cả các ký tự trước khi thêm khoảng trắng bằng cách sử dụng vòng lặp theo cách mạnh mẽ. Hàm isupper[] được sử dụng để kiểm tra ký tự viết hoa

    Python3




    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    9

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    0

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    1

     

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    2

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    3
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    4
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    5
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    0
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    1
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    2
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    1
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    4
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    5

     

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    6

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    7
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    8
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    9
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    0
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    1
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    2

     

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    3
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    4
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    5

     

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    6

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    7
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    8
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    9
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    90

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    91
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    92____34
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    94

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    91
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    7
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    97
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    9
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    99

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    00

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    00
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    02

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    00
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    04
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    05

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    06
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    07

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    06

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    00
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    10

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    00
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    12
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    13
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    14
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    15

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    91

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    91
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    18

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    91
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    20
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    21
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    22
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    7
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    8
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    9
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    26

     

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    27

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    7__
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    8
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    30
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    0
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    1
    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
    33

    Đầu ra

    The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
    The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']

    Phương pháp #2. Sử dụng regex[] + hiểu danh sách

    Sự kết hợp của các chức năng trên cũng có thể được sử dụng để giải quyết vấn đề này. Trong phần này, chúng tôi kiểm tra bất kỳ ký tự chữ hoa nào bằng cách sử dụng hàm isupper[] và sau đó thêm một khoảng trắng bổ sung bằng cách sử dụng hàm replace[]

    Chủ Đề