“cara mengganti string di python” Kode Jawaban

Python mengganti string

# string.replace(old, new, count),  no import is necessary
text = "Apples taste Good."
print(text.replace('Apples', 'Bananas'))          # use .replace() on a variable
Bananas taste Good.          <---- Output

print("Have a Bad Day!".replace("Bad","Good"))    # Use .replace() on a string
Have a Good Day!             <----- Output

print("Mom is happy!".replace("Mom","Dad").replace("happy","angry"))  #Use many times
Dad is angry!                <----- Output
Dr. Robert Brownell

Python cara mengganti string tertentu dalam teks

# Read in the file
with open('file.txt', 'r') as file :
  filedata = file.read()

# Replace the target string
filedata = filedata.replace('ram', 'abcd')

# Write the file out again
with open('file.txt', 'w') as file:
  file.write(filedata)
Brave Boar

cara mengganti string di python

# Replace a particular item in a Python list
a_list = ['aple', 'orange', 'aple', 'banana', 'grape', 'aple']
for i in range(len(a_list)):
    if a_list[i] == 'aple':
        a_list[i] = 'apple'
print(a_list)
# Returns: ['apple', 'orange', 'apple', 'banana', 'grape', 'apple']
Wide-eyed Wasp

cara mengganti string dalam python

# plz suscribe to my youtube channel -->
# https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A

text = "python is too difficult , I have a good experience with it"
#python is too difficult I am using this text for example only
print(text.replace('difficult', 'easy'))
#####output#####
#python is too easy , I have a good experience with it
Programmer of empires

Python String: .replace ()

# .replace() аргыг мөр доторх эхний аргументыг хоёр дахь аргументаар орлуулахад ашигладаг. 
# Эхний аргумент нь солигдох хуучин дэд мөр, хоёр дахь аргумент нь мөр доторх эхний аргумент бүрийг орлуулах шинэ дэд мөр юм.

fruit = "Strawberry"
print(fruit.replace('r', 'R'))
 
# StRawbeRRy
Puzzled Porcupine

Jawaban yang mirip dengan “cara mengganti string di python”

Pertanyaan yang mirip dengan “cara mengganti string di python”

Lebih banyak jawaban terkait untuk “cara mengganti string di python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya