Hướng dẫn how do you extract a letter from a string in python? - làm thế nào để bạn trích xuất một ký tự từ một chuỗi trong python?

Trong hướng dẫn này, chúng tôi sẽ học cách chỉ trích xuất các ký tự từ bất kỳ chuỗi nào trong Python. Chúng tôi sẽ tìm hiểu hai cách khác nhau để sử dụng hai phương pháp sau:python. We will learn two different ways of doing so using the following two method:

  1. ord(char)
  2. Enter a string: study123tonight
    0

Sử dụng Enter a string: study123tonight1

  • Nhận đầu vào từ người dùng bằng cách sử dụng
    Enter a string: study123tonight
    2Method.method.
  • Tuyên bố một chuỗi trống để lưu trữ bảng chữ cái.
  • Vòng lặp qua chuỗi:
    • Nếu giá trị ASCII của char nằm trong khoảng từ 65 đến 90 hoặc 97 đến 122. Sử dụng
      Enter a string: study123tonight
      3Method cho các giá trị ASCII của chars.ASCII value of char is between 65 and 90 or 97 and 122. Use the
      Enter a string: study123tonight
      3method for the ASCII values of chars.
      • Thêm nó vào chuỗi trống
  • In chuỗi kết quả.
## getting the input from the user
string = input("Enter a string: ")

## initializing a new string to apppend only alphabets
only_alpha = ""

## looping through the string to find out alphabets
for char in string:

## ord(chr) returns the ascii value
## CHECKING FOR UPPER CASE
if ord(char) >= 65 and ord(char) <= 90:
only_alpha += char
## checking for lower case
elif ord(char) >= 97 and ord(char) <= 122:
only_alpha += char

## printing the string which contains only alphabets
print(only_alpha)

Input:

Enter a string: study123tonight

Đầu ra của chương trình:

studytonight

Sử dụng Enter a string: study123tonight4

  • Nhận đầu vào từ người dùng bằng cách sử dụng
    Enter a string: study123tonight
    2Method.method.
  • Tuyên bố một chuỗi trống để lưu trữ bảng chữ cái.
  • Vòng lặp qua chuỗi:
    • Nếu giá trị ASCII của char nằm trong khoảng từ 65 đến 90 hoặc 97 đến 122. Sử dụng
      Enter a string: study123tonight
      3Method cho các giá trị ASCII của chars. method.
      • Thêm nó vào chuỗi trống
  • In chuỗi kết quả.
## get the input from the user
string = input("Enter a string: ")

## initializing a new string to append only alphabets
only_alpha = ""

## looping through the string to find out alphabets
for char in string:

## checking whether the char is an alphabet or not using chr.isalpha() method
if char.isalpha():
only_alpha += char

## printing the string which contains only alphabets
print(only_alpha)

Input:

Enter a string: study123tonight

Đầu ra của chương trình:

studytonight

Sử dụng

Enter a string: study123tonight
4

Kiểm tra xem char là bảng chữ cái hay không sử dụng phương pháp Enter a string: study123tonight4.

  • Thêm nó vào chuỗi trống.
  • Nếu bạn có bất kỳ truy vấn nào liên quan đến các chương trình, xin vui lòng cho tôi biết trong phần bình luận bên dưới.
  • Bạn cũng có thể thích:
  • Tiện ích nhãn Python Tkinter

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

    Đưa ra một chuỗi, nhiệm vụ là chỉ trích xuất các ký tự bảng chữ cái từ một chuỗi. Đưa ra dưới đây là một số phương pháp để giải quyết vấn đề đã cho.

    Phương pháp số 1: Sử dụng

    Enter a string: study123tonight
    7

    Enter a string: study123tonight
    8
    Enter a string: study123tonight
    9

    studytonight
    0
    studytonight
    1
    studytonight
    2

    studytonight
    3
    studytonight
    1
    studytonight
    5

    studytonight
    6
    studytonight
    7
    studytonight
    8
    studytonight
    9

    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    0____21
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    2
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    3
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    4
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    5

    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    6
    studytonight
    1
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    2
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    3
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    4
    Enter a string: study123tonight
    1

    Output:

    initial string :  123()#$ABGFDabcjw abceddfgh
    first string result:   ABGFDabcjw
    second string result:  abceddfgh

    studytonight
    6
    studytonight
    7
    Enter a string: study123tonight
    4
    Enter a string: study123tonight
    5
    Enter a string: study123tonight
    6
    Enter a string: study123tonight
    7
    Method #2: Using
    studytonight
    4

    Đưa ra một chuỗi, nhiệm vụ là chỉ trích xuất các ký tự bảng chữ cái từ một chuỗi. Đưa ra dưới đây là một số phương pháp để giải quyết vấn đề đã cho.

    Phương pháp số 1: Sử dụng

    Enter a string: study123tonight
    7

    Enter a string: study123tonight
    8
    Enter a string: study123tonight
    9

    studytonight
    0
    studytonight
    1
    studytonight
    2

    studytonight
    3
    studytonight
    1
    studytonight
    5

    studytonight
    6
    studytonight
    7
    studytonight
    8
    studytonight
    9

    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    0____21
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    2
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    3
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    4
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    5

    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    6
    studytonight
    1
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    2
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    3
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    4
    Enter a string: study123tonight
    1

    Output:

    initial string : 
     123()#$ABGFDabcjw 
     abceddfgh
    
    first string result:  ABGFDabcjw
    second string result:  abceddfgh

    studytonight
    6
    studytonight
    7
    Enter a string: study123tonight
    4
    Enter a string: study123tonight
    5
    Enter a string: study123tonight
    6
    Enter a string: study123tonight
    7
    Method #3: Using ord(char)3

    Phương pháp số 1: Sử dụng

    Enter a string: study123tonight
    7

    Enter a string: study123tonight
    8
    Enter a string: study123tonight
    9

    studytonight
    0
    studytonight
    1
    studytonight
    2

    studytonight
    3
    studytonight
    1
    studytonight
    5

    studytonight
    6
    studytonight
    7
    studytonight
    8
    studytonight
    9

    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    0____21
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    2
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    3
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    4
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    5

    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    6
    studytonight
    1
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    2
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    3
    ## get the input from the user
    string = input("Enter a string: ")
    
    ## initializing a new string to append only alphabets
    only_alpha = ""
    
    ## looping through the string to find out alphabets
    for char in string:
    
    ## checking whether the char is an alphabet or not using chr.isalpha() method
    if char.isalpha():
    only_alpha += char
    
    ## printing the string which contains only alphabets
    print(only_alpha)
    4
    Enter a string: study123tonight
    1

    Output:

    initial string :  123()#$ABGFDabcjw
    first result:  ABGFDabcjw
    

    Làm thế nào để bạn trích xuất một chữ cái từ một chuỗi?

    Sử dụng chuỗi ...
    Nhận chuỗi và chỉ mục ..
    Tạo một mảng char trống có kích thước 1 ..
    Sao chép phần tử tại chỉ mục cụ thể từ chuỗi vào char [] bằng chuỗi.phương thức getchars () ..
    Nhận ký tự cụ thể ở chỉ mục 0 của mảng ký tự ..
    Trả về nhân vật cụ thể ..

    Làm cách nào để trích xuất một phần cụ thể của chuỗi trong Python?

    Nhận một chuỗi con của một chuỗi đang trích xuất một phần của chuỗi từ một đối tượng chuỗi.Nó cũng được gọi là một hoạt động cắt lát.Bạn có thể nhận được chuỗi con của một chuỗi trong Python bằng tùy chọn STR [0: N].using the str[0:n] option.