Bagaimana cara mengubah satu-satunya kejadian kedua dari string di python?

Saat menggunakan metode

phrase = "I like to learn coding on the go"

# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a" )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading an the ga
0 Python, Anda dapat mengganti setiap instance dari satu karakter tertentu dengan yang baru. Anda bahkan dapat mengganti seluruh rangkaian teks dengan baris teks baru yang Anda tentukan

Metode

phrase = "I like to learn coding on the go"

# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a" )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading an the ga
0 mengembalikan salinan string. Ini berarti bahwa substring lama tetap sama, tetapi salinan baru dibuat – dengan semua teks lama telah diganti dengan teks baru

Bagaimana cara kerja metode Python phrase = "I like to learn coding on the go" # replace all instances of 'o' with 'a' substituted_phrase = phrase.replace("o", "a" ) print(phrase) print(substituted_phrase) #output #I like to learn coding on the go #I like ta learn cading an the ga 0?

Sintaks untuk metode

phrase = "I like to learn coding on the go"

# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a" )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading an the ga
0 terlihat seperti ini

string.replace(old_text, new_text, count)
_

Mari kita hancurkan

  • phrase = "I like to learn coding on the go"
    
    # replace all instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a" )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading an the ga
    
    6 adalah parameter wajib pertama yang diterima
    phrase = "I like to learn coding on the go"
    
    # replace all instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a" )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading an the ga
    
    0. Itu adalah karakter atau teks lama yang ingin Anda ganti. Lampirkan ini dalam tanda kutip
  • phrase = "I like to learn coding on the go"
    
    # replace all instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a" )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading an the ga
    
    8 adalah parameter wajib kedua yang diterima
    phrase = "I like to learn coding on the go"
    
    # replace all instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a" )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading an the ga
    
    0. Ini adalah karakter atau teks baru yang ingin Anda ganti dengan karakter/teks lama. Parameter ini juga perlu dilampirkan dalam tanda kutip
  • phrase = "I like to learn coding on the go"
    
    # replace only the first two instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a", 2 )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading on the go
    
    0 adalah parameter ketiga opsional yang diterima
    phrase = "I like to learn coding on the go"
    
    # replace all instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a" )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading an the ga
    
    0. Secara default,
    phrase = "I like to learn coding on the go"
    
    # replace all instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a" )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading an the ga
    
    0 akan menggantikan semua contoh substring. Namun, Anda dapat menggunakan
    phrase = "I like to learn coding on the go"
    
    # replace only the first two instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a", 2 )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading on the go
    
    _0 untuk menentukan jumlah kemunculan yang ingin Anda ganti

Python phrase = "I like to learn coding on the go" # replace all instances of 'o' with 'a' substituted_phrase = phrase.replace("o", "a" ) print(phrase) print(substituted_phrase) #output #I like to learn coding on the go #I like ta learn cading an the ga 0 Contoh Kode Metode

Cara Mengganti Semua Contoh Karakter Tunggal

Untuk mengubah semua instance dari satu karakter, Anda akan melakukan hal berikut

phrase = "I like to learn coding on the go"

# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a" )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading an the ga

Pada contoh di atas, setiap kata yang mengandung karakter

phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
5 diganti dengan karakter
phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
6

Dalam contoh itu ada empat contoh karakter

phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
5. Secara khusus, itu ditemukan dalam kata-kata
phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
_8,
phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
9,
phrase = "I like to learn coding on the go"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn coding on the go
0, dan
phrase = "I like to learn coding on the go"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn coding on the go
1

Bagaimana jika Anda hanya ingin mengubah dua kata, seperti

phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
8 dan
phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
_9, mengandung
phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
6 bukan
phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
5?

Cara Mengganti Hanya Sejumlah Instance dari Karakter Tunggal

Untuk mengubah hanya dua contoh dari satu karakter, Anda akan menggunakan parameter

phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
0 dan mengaturnya menjadi dua

phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
_

Jika Anda hanya ingin mengubah instance pertama dari satu karakter, Anda akan menyetel parameter

phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
0 menjadi satu

phrase = "I like to learn coding on the go"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn coding on the go

Cara Mengganti Semua Contoh String

Untuk mengubah lebih dari satu karakter, prosesnya terlihat serupa

phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.

Pada contoh di atas, kata

phrase = "I like to learn coding on the go"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn coding on the go
_8 diganti dengan kata
phrase = "I like to learn coding on the go"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn coding on the go
9

Cara Mengganti Hanya Sejumlah Instance tertentu dari sebuah String

Jika Anda hanya ingin mengubah instance pertama dari

phrase = "I like to learn coding on the go"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn coding on the go
8 menjadi
phrase = "I like to learn coding on the go"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn coding on the go
9, Anda akan menggunakan parameter
phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading on the go
0 dan menyetelnya ke satu

phrase = "The sun is strong today. I don't really like sun."

#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind", 1)

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.

Cara Melakukan Substitusi Substring Case-Insensitive dengan Python

Mari kita lihat contoh lain


phrase = "I am learning Ruby. I really enjoy the ruby programming language!"

#replace the text "Ruby" with "Python"
substituted_text = phrase.replace("Ruby", "Python")

print(substituted_text)

#output

#I am learning Python. I really enjoy the ruby programming language!

Dalam hal ini, yang benar-benar ingin saya lakukan adalah mengganti semua contoh kata

phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
3 dengan
phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
4

Namun, ada kata

phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
_5 dengan huruf kecil
phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
6, yang juga ingin saya ubah

Karena huruf pertama menggunakan huruf kecil, bukan huruf besar seperti yang saya tentukan dengan

phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
3, tetap sama dan tidak berubah menjadi
phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
4

Metode

phrase = "I like to learn coding on the go"

# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a" )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading an the ga
0 peka terhadap huruf besar-kecil, dan karenanya melakukan substitusi substring peka huruf besar-kecil

Untuk melakukan substitusi substring case-insensitive Anda harus melakukan sesuatu yang berbeda

Anda perlu menggunakan fungsi

phrase = "The sun is strong today. I don't really like sun."

#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind", 1)

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
_0 dan menggunakan flag
phrase = "The sun is strong today. I don't really like sun."

#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind", 1)

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
1

Untuk menggunakan

phrase = "The sun is strong today. I don't really like sun."

#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind", 1)

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
_0 Anda harus

  • Gunakan modul
    phrase = "The sun is strong today. I don't really like sun."
    
    #replace only the first instance of the word 'sun' with 'wind'
    substituted_phrase = phrase.replace("sun", "wind", 1)
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #The sun is strong today. I don't really like sun.
    #The wind is strong today. I don't really like sun.
    
    _3, melalui
    phrase = "The sun is strong today. I don't really like sun."
    
    #replace only the first instance of the word 'sun' with 'wind'
    substituted_phrase = phrase.replace("sun", "wind", 1)
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #The sun is strong today. I don't really like sun.
    #The wind is strong today. I don't really like sun.
    
    4
  • Tentukan ekspresi reguler
    phrase = "The sun is strong today. I don't really like sun."
    
    #replace only the first instance of the word 'sun' with 'wind'
    substituted_phrase = phrase.replace("sun", "wind", 1)
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #The sun is strong today. I don't really like sun.
    #The wind is strong today. I don't really like sun.
    
    5
  • Sebutkan dengan apa yang ingin Anda
    phrase = "The sun is strong today. I don't really like sun."
    
    #replace only the first instance of the word 'sun' with 'wind'
    substituted_phrase = phrase.replace("sun", "wind", 1)
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #The sun is strong today. I don't really like sun.
    #The wind is strong today. I don't really like sun.
    
    6 polanya
  • Sebutkan
    phrase = "The sun is strong today. I don't really like sun."
    
    #replace only the first instance of the word 'sun' with 'wind'
    substituted_phrase = phrase.replace("sun", "wind", 1)
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #The sun is strong today. I don't really like sun.
    #The wind is strong today. I don't really like sun.
    
    7 tempat Anda ingin melakukan operasi ini
  • Opsional, tentukan parameter
    phrase = "I like to learn coding on the go"
    
    # replace only the first two instances of 'o' with 'a'
    substituted_phrase = phrase.replace("o", "a", 2 )
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #I like to learn coding on the go
    #I like ta learn cading on the go
    
    _0 untuk membuat penggantian lebih tepat dan tentukan jumlah maksimum penggantian yang ingin Anda lakukan
  • Bendera
    phrase = "The sun is strong today. I don't really like sun."
    
    #replace only the first instance of the word 'sun' with 'wind'
    substituted_phrase = phrase.replace("sun", "wind", 1)
    
    print(phrase)
    print(substituted_phrase)
    
    #output
    
    #The sun is strong today. I don't really like sun.
    #The wind is strong today. I don't really like sun.
    
    _1 memberi tahu ekspresi reguler untuk melakukan pencocokan case-insensitive

Jadi, secara keseluruhan sintaksnya terlihat seperti ini

import re

re.sub(pattern, replace, string, count, flags) 

Mengambil contoh dari sebelumnya

phrase = "I am learning Ruby. I really enjoy the ruby programming language!"

Beginilah cara saya mengganti

phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
_3 dan
phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
5 dengan
phrase = "The sun is strong today. I don't really like sun."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
4

import re

phrase = "I am learning Ruby. I really enjoy the ruby programming language!"

phrase = re.sub("Ruby","Python", phrase, flags=re.IGNORECASE)

print(phrase)

#output

#I am learning Python. I really enjoy the Python programming language!

Membungkus

Dan begitulah - Anda sekarang tahu dasar-dasar substitusi substring. Semoga panduan ini bermanfaat bagi Anda

Untuk mempelajari lebih lanjut tentang Python, lihat Komputasi Ilmiah freeCodeCamp dengan Sertifikasi Python

Anda akan mulai dari dasar dan belajar dengan cara yang interaktif dan ramah bagi pemula. Anda juga akan membangun lima proyek pada akhirnya untuk dipraktikkan dan membantu memperkuat apa yang Anda pelajari

Terima kasih telah membaca dan selamat membuat kode

IKLAN

IKLAN

IKLAN

IKLAN


Bagaimana cara mengubah satu-satunya kejadian kedua dari string di python?
Dionysia Lemonaki

Mempelajari sesuatu yang baru setiap hari dan menulis tentangnya


Jika artikel ini bermanfaat, tweetlah

Belajar kode secara gratis. Kurikulum open source freeCodeCamp telah membantu lebih dari 40.000 orang mendapatkan pekerjaan sebagai pengembang. Memulai

Bagaimana cara mengganti kemunculan karakter kedua dalam string Python?

Memasukkan. S = “GeeksforGeeks”, ch[] = {'G', 'e', ​​'k'}, N = 2, replace_character = '#'
Keluaran. Ge#ksfor#ee#s
Penjelasan. Dalam string S yang diberikan, kemunculan kedua dari 'G', 'e', ​​'K' diganti dengan '#'

Bagaimana Anda mengganti kejadian tertentu dengan Python?

Python String replace() Metode . Catatan. Semua kemunculan frasa yang ditentukan akan diganti, jika tidak ada lagi yang ditentukan. The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.

Bagaimana Anda mengganti hanya satu kemunculan string dengan Python?

replace (old, new[, count]) -> string Kembalikan salinan string S dengan semua kemunculan substring lama diganti dengan yang baru. Jika hitungan argumen opsional diberikan, hanya kemunculan hitungan pertama yang diganti.

Bagaimana Anda mendapatkan kejadian kedua dengan Python?

Gunakan metode find() .
def find_string(txt, str1)
mengembalikan txt. temukan(str1, txt. temukan(str1)+1)
string = input("Masukkan string. ")
substring = input("Masukkan substring. ")
print("Indeks kemunculan kedua dari substring. ", find_string(string, substring))