पायथन स्ट्रिंग मेकट्रांस () विधि
उदाहरण
मैपिंग टेबल बनाएं, और translate()
किसी भी "एस" वर्णों को "पी" वर्ण से बदलने के लिए विधि में इसका उपयोग करें:
txt = "Hello Sam!"
mytable = txt.maketrans("S", "P")
print(txt.translate(mytable))
परिभाषा और उपयोग
विधि एक maketrans()
मैपिंग तालिका लौटाती है जिसका उपयोग निर्दिष्ट वर्णों को बदलने के लिए विधि के साथ किया जा सकता है।
translate()
वाक्य - विन्यास
string.maketrans(x, y, z)
पैरामीटर मान
Parameter | Description |
---|---|
x | Required. If only one parameter is specified, this has to be a dictionary describing how to perform the replace. If two or more parameters are specified, this parameter has to be a string specifying the characters you want to replace. |
y | Optional. A string with the same length as parameter x. Each character in the first parameter will be replaced with the corresponding character in this string. |
z | Optional. A string describing which characters to remove from the original string. |
और ज्यादा उदाहरण
उदाहरण
कई वर्णों को बदलने के लिए मानचित्रण तालिका का उपयोग करें:
txt = "Hi Sam!"
x = "mSa"
y = "eJo"
mytable = txt.maketrans(x,
y)
print(txt.translate(mytable))
उदाहरण
मैपिंग तालिका में तीसरा पैरामीटर उन वर्णों का वर्णन करता है जिन्हें आप स्ट्रिंग से हटाना चाहते हैं:
txt = "Good night Sam!"
x = "mSa"
y = "eJo"
z = "odnght"
mytable = txt.maketrans(x, y, z)
print(txt.translate(mytable))
उदाहरण
विधि स्वयं यूनिकोड में प्रत्येक प्रतिस्थापन का वर्णन करने वाला maketrans()
एक शब्दकोश लौटाती है:
txt = "Good night Sam!"
x = "mSa"
y = "eJo"
z = "odnght"
print(txt.maketrans(x, y, z))