സ്ട്രിംഗ് ഫോർമാറ്റിംഗ്#

ആമുഖം#

പൈത്തണിലെ .format() രീതി വേരിയബിളുകൾ, എക്സ്പ്രഷനുകൾ അല്ലെങ്കിൽ മൂല്യങ്ങൾ എന്നിവ ഉൾപ്പെടുന്ന സ്ട്രിംഗുകൾ സൃഷ്ടിക്കാൻ ഉപയോഗിക്കുന്നു. ഈ രീതി ഒരു സ്ട്രിംഗിലെ ചുരുണ്ട ബ്രേസ് പ്ലെയ്‌സ്‌ഹോൾഡറുകൾ {} നെ .format() നുള്ളിൽ നൽകിയിരിക്കുന്ന മൂല്യങ്ങൾ ഉപയോഗിച്ച് മാറ്റിസ്ഥാപിക്കുന്നു.

def main():
    x = 1
    y = 5

    # Display the variables using .format()
    console.print("Position: ({}, {})".format(x, y))

# Start threads — Do not delete
start_thread(main)

def main():
    # Display a calculation using .format()
    console.print("Sum: {}".format(5 + 3))

# Start threads — Do not delete
start_thread(main)

def main():
    wait(1, SECONDS)
    # Display the time using .format()
    console.print("Time: {}".format(timer.time(SECONDS)))

# Start threads — Do not delete
start_thread(main)

.format ഉപയോഗിച്ച് സംഖ്യകൾ ഫോർമാറ്റ് ചെയ്യുന്നു – ഒരു സ്ട്രിംഗിലേക്ക് മൂല്യങ്ങളും എക്സ്പ്രഷനുകളും ചേർക്കുക.

  • :.xf – കാണിക്കേണ്ട ദശാംശസ്ഥാനങ്ങളുടെ എണ്ണം സജ്ജമാക്കുന്നു.

  • round – ഒരു സംഖ്യയെ നൽകിയിരിക്കുന്ന ദശാംശ സ്ഥാനങ്ങളിലേക്ക് റൗണ്ട് ചെയ്യുന്നു.

  • :, – ആയിരങ്ങളുടെ വിഭജനമായി കോമകൾ ചേർക്കുന്നു.

  • :.x% – ഒരു ദശാംശത്തെ x ദശാംശ സ്ഥാനങ്ങളുള്ള ശതമാനമാക്കി മാറ്റുന്നു.

  • :#x – ഒരു സംഖ്യയെ ഹെക്സാഡെസിമൽ ആയി ഫോർമാറ്റ് ചെയ്യുന്നു.

  • :b – ഒരു സംഖ്യയെ ബൈനറി ആയി ഫോർമാറ്റ് ചെയ്യുന്നു.

സ്ട്രിംഗുകൾ സംയോജിപ്പിക്കൽ - വാചകവും മൂല്യങ്ങളും സംയോജിപ്പിക്കുക.

  • .format() – സ്ട്രിംഗുകളും വേരിയബിളുകളും ഒരൊറ്റ എക്സ്പ്രഷനിൽ സംയോജിപ്പിക്കുക.

  • + operator – ഓപ്ഷണൽ ടൈപ്പ് കൺവേർഷൻ ഉപയോഗിച്ച് സ്ട്രിംഗുകൾ സ്വമേധയാ ബന്ധിപ്പിക്കുക.

സ്ട്രിംഗ് രീതികൾ – വാചകത്തിന്റെ കേസ് മാറ്റുക.

  • upper() – എല്ലാ പ്രതീകങ്ങളെയും വലിയക്ഷരത്തിലേക്ക് പരിവർത്തനം ചെയ്യുന്നു.

  • lower() – എല്ലാ പ്രതീകങ്ങളെയും ചെറിയക്ഷരത്തിലേക്ക് പരിവർത്തനം ചെയ്യുന്നു.

സബ്‌സ്ട്രിംഗുകൾ പരിശോധിക്കുന്നു - വാചകത്തിന്റെ സാന്നിധ്യമോ സ്ഥാനമോ പരിശോധിക്കുന്നു.

  • in – Checks if a word exists in a string.

  • startswith() – ഒരു സ്ട്രിംഗ് നൽകിയിരിക്കുന്ന മൂല്യത്തിൽ ആരംഭിക്കുന്നുണ്ടോ എന്ന് പരിശോധിക്കുന്നു.

  • endswith() – ഒരു സ്ട്രിംഗ് നൽകിയിരിക്കുന്ന മൂല്യത്തിൽ അവസാനിക്കുന്നുണ്ടോ എന്ന് പരിശോധിക്കുന്നു.

എസ്കേപ്പ് സീക്വൻസുകൾ – പ്രത്യേക പ്രതീകങ്ങൾ ഉപയോഗിച്ച് ഔട്ട്പുട്ട് ഫോർമാറ്റ് ചെയ്യുക.

  • \n – ഒരു ലൈൻ ബ്രേക്ക് (പുതിയ ലൈൻ) ചേർക്കുന്നു.

  • \t – ഇനങ്ങൾക്കിടയിൽ ഒരു ടാബ് സ്പേസ് ചേർക്കുന്നു.

.format#

ദശാംശ സ്ഥാനങ്ങൾ, റൗണ്ടിംഗ്, മറ്റ് ഫോർമാറ്റിംഗ് ശൈലികൾ എന്നിവയുൾപ്പെടെ സംഖ്യകൾ എങ്ങനെ ദൃശ്യമാകുമെന്ന് നിയന്ത്രിക്കാൻ .format ഉപയോഗിക്കാം:

Fixed Decimal Places#

:.xf ഒരു സംഖ്യ എത്ര ദശാംശസ്ഥാനങ്ങൾ ഉപയോഗിച്ച് പ്രദർശിപ്പിക്കണമെന്ന് നിയന്ത്രിക്കുന്നു.

Usage:
:.xf

പാരാമീറ്ററുകൾ

വിവരണം

x

കാണിക്കേണ്ട ദശാംശസ്ഥാനങ്ങളുടെ എണ്ണം.

def main():
    # Display pi with 2 decimal places
    pi = 3.1415926535
    console.print("Pi: {:.2f}".format(pi))
    # Output: Pi: 3.14

# Start threads — Do not delete
start_thread(main)

Rounding Numbers#

ഫോർമാറ്റ് ചെയ്യുന്നതിന് മുമ്പ് round ഒരു സംഖ്യയെ ഒരു നിശ്ചിത സംഖ്യ ദശാംശ സ്ഥാനങ്ങളിലേക്ക് റൗണ്ട് ചെയ്യുന്നു.

Usage:
round(number, x)

പാരാമീറ്ററുകൾ

വിവരണം

number

റൗണ്ട് ചെയ്യേണ്ട സംഖ്യ.

x

റൗണ്ട് ചെയ്യേണ്ട ദശാംശ സ്ഥാനങ്ങളുടെ എണ്ണം.

def main():
    # Display a value rounded to only 2 decimal places
    value = 5.6789
    console.print("Rounded: {}".format(round(value, 2)))
    # Output: Rounded: 5.68

# Start threads — Do not delete
start_thread(main)

Thousands Separator#

:, വലിയ സംഖ്യകളെ കൂടുതൽ വായിക്കാൻ കഴിയുന്നതാക്കാൻ ആയിരങ്ങളുടെ വിഭജനമായി കോമകൾ ചേർക്കുന്നു.

Usage:
:,

പാരാമീറ്ററുകൾ

വിവരണം

ഈ ഫോർമാറ്റ് സ്പെസിഫയറിന് പാരാമീറ്ററുകളൊന്നുമില്ല.

def main():
    # Display a large number separated with commas
    number = 1234567
    console.print("Formatted: ")
    console.new_line()
    console.print("{:,}".format(number))
    # Output: Formatted: 1,234,567

# Start threads — Do not delete
start_thread(main)

Percentage#

:.x% ദശാംശ മൂല്യങ്ങളെ ശതമാനമായി ഫോർമാറ്റ് ചെയ്യുന്നത് മൂല്യത്തെ 100 കൊണ്ട് ഗുണിച്ച് ഒരു ശതമാന ചിഹ്നം ചേർത്താണ്.

Usage:
:.x%

പാരാമീറ്ററുകൾ

വിവരണം

x

കാണിക്കേണ്ട ദശാംശസ്ഥാനങ്ങളുടെ എണ്ണം.

def main():
    # Display a converted decimal to a percentage
    value = 0.875
    console.print("Score: {:.1%}".format(value))
    # Output: Score: 87.5%

# Start threads — Do not delete
start_thread(main)

Hexadecimal#

:#x സംഖ്യകളെ ഹെക്സാഡെസിമലിലേക്ക് പരിവർത്തനം ചെയ്യുന്നു.

Usage:
:#x

പാരാമീറ്ററുകൾ

വിവരണം

ഈ ഫോർമാറ്റ് സ്പെസിഫയറിന് പാരാമീറ്ററുകളൊന്നുമില്ല.

def main():
    # Convert 255 to hexadecimal
    number = 255
    console.print("Hex: {:#x}".format(number))
    # Output: Hex: 0xff

# Start threads — Do not delete
start_thread(main)

Binary#

:b സംഖ്യകളെ ബൈനറി (ബേസ് 2) ആക്കി മാറ്റുന്നു.

Usage:
:b

പാരാമീറ്ററുകൾ

വിവരണം

ഈ ഫോർമാറ്റ് സ്പെസിഫയറിന് പാരാമീറ്ററുകളൊന്നുമില്ല.

def main():
    # Convert 3 to binary
    console.print("Binary: {:b}".format(3))
    # Output: Binary: 11

# Start threads — Do not delete
start_thread(main)

സ്ട്രിങ്ങുകൾ സംയോജിപ്പിക്കൽ#

രണ്ട് സമീപനങ്ങൾ ഉപയോഗിച്ച് നിങ്ങൾക്ക് സ്ട്രിംഗുകൾ സംയോജിപ്പിക്കാം (അല്ലെങ്കിൽ സംയോജിപ്പിക്കാം):

Using .format#

{} ഉപയോഗിച്ച് സ്ട്രിംഗിലേക്ക് നേരിട്ട് മൂല്യങ്ങൾ ചേർക്കുക.

def main():
    # Display an answer based on the given emotion
    emotion = "good"
    console.print("I'm {}, you?".format(emotion))

# Start threads — Do not delete
start_thread(main)

+ Operator#

+ ഉപയോഗിച്ച് ഒന്നിലധികം ഭാഗങ്ങൾ കൂട്ടിച്ചേർക്കുക.

കുറിപ്പ്: ആദ്യം str() ഉപയോഗിച്ച് നോൺ-സ്ട്രിംഗുകൾ സ്ട്രിംഗുകളായി പരിവർത്തനം ചെയ്യണം.

def main():
    # Display the x and y values
    x = 10
    y = 20
    console.print("X: " + str(x) + ", Y: " + str(y))

# Start threads — Do not delete
start_thread(main)

സ്ട്രിംഗ് രീതികൾ#

സ്ട്രിങ്ങുകൾ പരിഷ്കരിക്കുന്നതിനും പരിശോധിക്കുന്നതിനുമുള്ള ബിൽറ്റ്-ഇൻ രീതികൾ പൈത്തൺ നൽകുന്നു.

upper#

upper ഒരു സ്ട്രിംഗിലെ എല്ലാ അക്ഷരങ്ങളെയും വലിയക്ഷരമാക്കി മാറ്റുന്നു.

Usage:
upper()

പാരാമീറ്ററുകൾ

വിവരണം

ഈ രീതിക്ക് പാരാമീറ്ററുകൾ ഇല്ല.

def main():
    # Capitalize a string with .upper()
    message = "vexcode"
    console.print(message.upper())  # Output: VEXCODE

# Start threads — Do not delete
start_thread(main)

lower#

lower ഒരു സ്ട്രിംഗിലെ എല്ലാ അക്ഷരങ്ങളെയും ചെറിയക്ഷരമാക്കി മാറ്റുന്നു.

Usage:
lower()

പാരാമീറ്ററുകൾ

വിവരണം

ഈ രീതിക്ക് പാരാമീറ്ററുകൾ ഇല്ല.

def main():
    # Make a string lowercase with .lower()
    message = "VEXCODE"
    console.print(message.lower())  # Output: vexcode

# Start threads — Do not delete
start_thread(main)

സബ്‌സ്ട്രിംഗുകൾ പരിശോധിക്കുന്നു#

in#

ഒരു സ്ട്രിംഗിൽ ഒരു സബ്‌സ്ട്രിംഗ് (വാക്ക് അല്ലെങ്കിൽ വാചക ഭാഗം) നിലവിലുണ്ടോ എന്ന് സൂചിപ്പിക്കുന്ന ഒരു ബൂളിയൻ തിരികെ നൽകുന്ന ഒരു കീവേഡാണ് in.

  • True – സ്ട്രിംഗിൽ ഈ വാക്ക് നിലവിലുണ്ട്.

  • False – സ്ട്രിംഗിൽ ആ വാക്ക് നിലവിലില്ല.

def main():
    message = "Hey everyone!"
    if "Hey" in message:
        console.print("Hello!")

# Start threads — Do not delete
start_thread(main)

startswith#

startswith returns a Boolean indicating whether a string begins with a given value.

  • True – വാക്ക് സ്ട്രിംഗ് ആരംഭിക്കുന്നു.

  • False – വാക്ക് സ്ട്രിംഗ് ആരംഭിക്കുന്നില്ല.

Usage:
startswith(substring)

പാരാമീറ്ററുകൾ

വിവരണം

substring

സ്ട്രിംഗിന്റെ തുടക്കത്തിൽ പരിശോധിക്കേണ്ട സബ്സ്ട്രിംഗ്.

def main():
    # Check for 'GO' at the start of a string
    message = "GO Robot"

    if message.startswith("GO"):
        console.print("GO first!")

# Start threads — Do not delete
start_thread(main)

endswith#

endswith returns a Boolean indicating whether a string ends with a given value.

  • True – വാക്ക് സ്ട്രിംഗ് അവസാനിപ്പിക്കുന്നു.

  • False – വാക്ക് സ്ട്രിംഗ് അവസാനിപ്പിക്കുന്നില്ല.

Usage:
endswith(substring)

പാരാമീറ്ററുകൾ

വിവരണം

substring

സ്ട്രിംഗിന്റെ അവസാനം പരിശോധിക്കേണ്ട സബ്സ്ട്രിംഗ്.

def main():
    # Check for `Robot` at the end of a string
    message = "GO Robot"

    if message.endswith("Robot"):
        console.print("Robot last!")

# Start threads — Do not delete
start_thread(main)

എസ്കേപ്പ് സീക്വൻസുകൾ#

ടെക്സ്റ്റ് ഔട്ട്പുട്ട് ഫോർമാറ്റ് ചെയ്യുന്നതിന് സ്ട്രിംഗുകൾക്കുള്ളിൽ ഉപയോഗിക്കുന്ന പ്രത്യേക പ്രതീകങ്ങളാണ് എസ്കേപ്പ് സീക്വൻസുകൾ.

New Line#

\n പ്രിന്റ് ചെയ്യുമ്പോൾ ടെക്സ്റ്റ് ഒരു പുതിയ വരിയിലേക്ക് നീക്കുന്നു.

def main():
    # Display text on two lines
    console.print("First line\nSecond line")

# Start threads — Do not delete
start_thread(main)

Tab Spacing#

\t വാക്കുകൾക്കോ ​​അക്കങ്ങൾക്കോ ​​ഇടയിൽ ഒരു ടാബ് സ്പേസ് ചേർക്കുന്നു.

def main():
    # Display the quantity of disks
    quantity = 2
    console.print("Disks:\t", quantity)

# Start threads — Do not delete
start_thread(main)