Hướng dẫn python lowercase the first letter - python viết thường chữ cái đầu tiên

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc

    Bàn luận
    This task can easily be performed using the lower method which lowercases the characters provided to it and slicing can be used to add the remaining string after the lowercase first character.

    Vấn đề viết hoa một chuỗi là khá phổ biến và đã được thảo luận nhiều lần. Nhưng đôi khi, chúng ta có thể có một vấn đề như thế này trong đó chúng ta cần chuyển đổi ký tự đầu tiên của chuỗi thành chữ thường. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện.

    Phương pháp số 1: Sử dụng Chuỗi cắt +

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    3, nhiệm vụ này có thể dễ dàng được thực hiện bằng phương pháp thấp hơn, các ký tự được cung cấp cho nó và cắt có thể được sử dụng để thêm chuỗi còn lại sau ký tự đầu tiên.

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    4
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    5
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    6

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    7
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    8
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    9
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    0
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    1
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    2

    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    3
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    5
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    5
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    6
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    7__
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    5
    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    
    0
    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    
    1

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    7
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    8
    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    
    4
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    0
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    1
    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    
    7

    The recipe of lambda function has to be added if we need to perform the task of handling None values or empty strings as well, and this becomes essential to handle edge cases.

    Đầu ra:

    Phương pháp số 2: Sử dụng Lambda + Chuỗi cắt +

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    3 Công thức của chức năng Lambda phải được thêm vào nếu chúng ta cần thực hiện nhiệm vụ xử lý không có giá trị hoặc chuỗi trống, và điều này trở nên cần thiết để xử lý các trường hợp cạnh.

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    4
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    5
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    6

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    7
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    8
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    9
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    0
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    1
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    2

    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    3
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    5
    def uncapitalize(s):
      if len(s) > 0:
        s = s[0].lower() + s[1:].upper()
      return s
    
    0
    def uncapitalize(s):
      if len(s) > 0:
        s = s[0].lower() + s[1:].upper()
      return s
    
    1
    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    
    0
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    7
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    0

    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    3
    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    5
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    5
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    6
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    7__
    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    
    5
    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    
    0
    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    
    1

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    


    Có một chức năng để tận dụng một chuỗi, tôi muốn có thể thay đổi ký tự đầu tiên của một chuỗi để chắc chắn rằng nó sẽ là chữ thường.

    Làm thế nào tôi có thể làm điều đó trong Python?

    Hướng dẫn python lowercase the first letter - python viết thường chữ cái đầu tiên

    Martineau

    Huy hiệu vàng 115K2525 gold badges160 silver badges284 bronze badges

    Đã hỏi ngày 1 tháng 10 năm 2010 lúc 15:52Oct 1, 2010 at 15:52

    Hướng dẫn python lowercase the first letter - python viết thường chữ cái đầu tiên

    2

    One-liner xử lý các chuỗi trống và

    >>> mystring = 'ABCDE'
    >>> mystring[0].lower() + mystring[1:]
    'aBCDE'
    >>> 
    
    9:

    func = lambda s: s[:1].lower() + s[1:] if s else ''
    
    >>> func(None)
    >>> ''
    >>> func('')
    >>> ''
    >>> func('MARTINEAU')
    >>> 'mARTINEAU'
    

    Đã trả lời ngày 2 tháng 10 năm 2010 lúc 20:16Oct 2, 2010 at 20:16

    Hướng dẫn python lowercase the first letter - python viết thường chữ cái đầu tiên

    Martineaumartineaumartineau

    Huy hiệu vàng 115K2525 gold badges160 silver badges284 bronze badges

    8

    s = "Bobby tables"
    s = s[0].lower() + s[1:]
    

    Đã hỏi ngày 1 tháng 10 năm 2010 lúc 15:52Oct 1, 2010 at 15:54

    One-liner xử lý các chuỗi trống và

    >>> mystring = 'ABCDE'
    >>> mystring[0].lower() + mystring[1:]
    'aBCDE'
    >>> 
    
    9:JoshD

    Đã trả lời ngày 2 tháng 10 năm 2010 lúc 20:162 gold badges42 silver badges53 bronze badges

    2

    def first_lower(s):
       if len(s) == 0:
          return s
       else:
          return s[0].lower() + s[1:]
    
    print first_lower("HELLO")  # Prints "hELLO"
    print first_lower("")       # Doesn't crash  :-)
    

    MartineaumartineauOct 1, 2010 at 15:53

    Đã trả lời ngày 1 tháng 10 năm 2010 lúc 15:54RichieHindle

    Joshdjoshd46 gold badges353 silver badges394 bronze badges

    12

    12.2k2 Huy hiệu vàng42 Huy hiệu bạc53 Huy hiệu Đồng

    def uncapitalize(s):
      if len(s) > 0:
        s = s[0].lower() + s[1:].upper()
      return s
    

    Đã trả lời ngày 1 tháng 10 năm 2010 lúc 15:53Oct 1, 2010 at 16:18

    Hướng dẫn python lowercase the first letter - python viết thường chữ cái đầu tiên

    RichiehindlerichiehindleAdrian McCarthy

    263K46 Huy hiệu vàng353 Huy hiệu bạc394 Huy hiệu Đồng16 gold badges121 silver badges171 bronze badges

    2

    Thật thú vị, không có câu trả lời nào trong số này làm chính xác điều ngược lại với

    >>> def first_lower(s):
       if not s: # Added to handle case where s == None
       return 
       else:
          return s[0].lower() + s[1:]
    
    >>> first_lower(None)
    >>> first_lower("HELLO")
    'hELLO'
    >>> first_lower("")
    >>> 
    
    0. Ví dụ:
    >>> def first_lower(s):
       if not s: # Added to handle case where s == None
       return 
       else:
          return s[0].lower() + s[1:]
    
    >>> first_lower(None)
    >>> first_lower("HELLO")
    'hELLO'
    >>> first_lower("")
    >>> 
    
    1 trả về
    >>> def first_lower(s):
       if not s: # Added to handle case where s == None
       return 
       else:
          return s[0].lower() + s[1:]
    
    >>> first_lower(None)
    >>> first_lower("HELLO")
    'hELLO'
    >>> first_lower("")
    >>> 
    
    2 thay vì
    >>> def first_lower(s):
       if not s: # Added to handle case where s == None
       return 
       else:
          return s[0].lower() + s[1:]
    
    >>> first_lower(None)
    >>> first_lower("HELLO")
    'hELLO'
    >>> first_lower("")
    >>> 
    
    3. Nếu bạn muốn ngược lại với
    >>> def first_lower(s):
       if not s: # Added to handle case where s == None
       return 
       else:
          return s[0].lower() + s[1:]
    
    >>> first_lower(None)
    >>> first_lower("HELLO")
    'hELLO'
    >>> first_lower("")
    >>> 
    
    0, bạn cần một cái gì đó như:

    >>> mystring = 'ABCDE'
    >>> mystring[0].lower() + mystring[1:]
    'aBCDE'
    >>> 
    

    Đã trả lời ngày 1 tháng 10 năm 2010 lúc 16:18

    Adrian McCarthyadrian McCarthy

    >>> def first_lower(s):
       if not s: # Added to handle case where s == None
       return 
       else:
          return s[0].lower() + s[1:]
    
    >>> first_lower(None)
    >>> first_lower("HELLO")
    'hELLO'
    >>> first_lower("")
    >>> 
    

    Đã hỏi ngày 1 tháng 10 năm 2010 lúc 15:52Oct 1, 2010 at 15:54

    One-liner xử lý các chuỗi trống và

    >>> mystring = 'ABCDE'
    >>> mystring[0].lower() + mystring[1:]
    'aBCDE'
    >>> 
    
    9:Manoj Govindan

    69,7K21 Huy hiệu vàng131 Huy hiệu bạc138 Huy hiệu đồng21 gold badges131 silver badges138 bronze badges

    2

    Không cần phải xử lý các trường hợp đặc biệt (và tôi nghĩ rằng sự đối xứng là Pythonic hơn):

    def uncapitalize(s):
        return s[:1].lower() + s[1:].upper()
    

    Đã trả lời ngày 2 tháng 10 năm 2010 lúc 5:58Oct 2, 2010 at 5:58

    Don O'Donnelldon O'DonnellDon O'Donnell

    4.4283 Huy hiệu vàng25 Huy hiệu bạc27 Huy hiệu đồng3 gold badges25 silver badges27 bronze badges

    3

    Tôi sẽ viết nó theo cách này:

    def first_lower(s):
        if s == "":
            return s
        return s[0].lower() + s[1:]
    

    Điều này có giá trị (tương đối) rằng nó sẽ ném lỗi nếu bạn vô tình chuyển nó một thứ không phải là một chuỗi, như

    >>> mystring = 'ABCDE'
    >>> mystring[0].lower() + mystring[1:]
    'aBCDE'
    >>> 
    
    9 hoặc một danh sách trống.

    Đã trả lời ngày 1 tháng 10 năm 2010 lúc 22:57Oct 1, 2010 at 22:57

    Robert Rossneyrobert RossneyRobert Rossney

    92.6K24 Huy hiệu vàng143 Huy hiệu bạc216 Huy hiệu đồng24 gold badges143 silver badges216 bronze badges

    2

    Bài viết trùng lặp này dẫn tôi đến đây.

    Nếu bạn có một danh sách các chuỗi như bảng hiển thị bên dưới

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    0

    Sau đó, để chuyển đổi chữ cái đầu tiên của tất cả các mục trong danh sách, bạn có thể sử dụng

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    1

    Đầu ra

    The original string is : GeeksforGeeks
    The string after lowercasing initial character : geeksforGeeks
    
    2

    Đã trả lời ngày 13 tháng 11 năm 2017 lúc 20:32Nov 13, 2017 at 20:32

    Hướng dẫn python lowercase the first letter - python viết thường chữ cái đầu tiên

    Van Peervan ngang hàngVan Peer

    2.0672 Huy hiệu vàng22 Huy hiệu bạc34 Huy hiệu đồng2 gold badges22 silver badges34 bronze badges

    1

    Làm cách nào để thay đổi chữ cái đầu tiên thành chữ thường trong Python?

    Python: Decapitalize chữ cái đầu tiên của một chuỗi nhất định..
    Sử dụng danh sách cắt và str. thấp hơn () để giải mã chữ cái đầu tiên của chuỗi ..
    Sử dụng str. ....
    Bỏ qua tham số Upper_rest để giữ cho phần còn lại của chuỗi nguyên vẹn hoặc đặt nó thành True để chuyển đổi thành chữ hoa ..

    Làm thế nào để bạn viết chữ cái đầu tiên của một chuỗi thành chữ thường?

    Cách hiệu quả nhất để tạo nhân vật đầu tiên của một trường hợp thường xuyên là gì?Chuỗi đầu vào = "someInputString";char c [] = input.toararray ();c [0] = ký tự.toLowerCase (c [0]);Chuỗi đầu ra = chuỗi mới (c);String input = "SomeInputString"; char c[] = input. toCharArray(); c[0] = Character. toLowerCase(c[0]); String output = new String(c);

    Làm thế nào để bạn viết hoa chữ cái đầu tiên và dấu thường nghỉ ngơi trong Python?

    Python có một phương thức tích hợp có tên incessize () để chuyển đổi ký tự đầu tiên của chuỗi thành chữ hoa và thay đổi phần còn lại của các ký tự thành chữ thường.Phương pháp này có thể được sử dụng trên dữ liệu chuỗi theo nhiều cách khác nhau mà không chỉ tận dụng các ký tự đầu tiên.capitalize() to convert the first character of a string into uppercase and change the rest of the characters into lowercase. This method can be used on string data in various ways without just capitalizing on the first characters.

    Làm thế nào để bạn thoát khỏi chữ cái đầu tiên trong một chuỗi trăn?

    Xóa ký tự đầu tiên khỏi chuỗi trong python bằng phương thức str.lStrip ().STR.Phương thức lStrip () lấy một hoặc nhiều ký tự làm đầu vào, loại bỏ chúng từ đầu chuỗi và trả về một chuỗi mới với các ký tự bị xóa.Using the str. lstrip() Method. The str. lstrip() method takes one or more characters as input, removes them from the start of the string, and returns a new string with removed characters.