Python for Beginners [12 of 44] Demo: Formatting Strings

Play Python for Beginners [12 of 44] Demo: Formatting Strings
Sign in to queue

Description

Demo: Using various string formatting options.

Full 'Intro to Python' course on Microsoft Learn: https://aka.ms/MSLearnPython

Sample code: https://aka.ms/PythonGettingStarted
Watch the entire series: https://aka.ms/PythonBeginnerSeries

 

Tag:

Python

Embed

Download

The Discussion

  • User profile image
    Kerala

    this is the best class for python beginners as it explains everything in detailed.
    thanks, Microsoft and channel9

  • User profile image
    AAEadmin
    the f construct is not working for me, it gives this error:

    output = f'Hello, {first_name} {last_name}'
    ^
    SyntaxError: invalid syntax
  • User profile image
    Gofightnguy​en
    @AAEadmin: What version of python? It needs to be 3+. You can run `python --version` to see
  • User profile image
    anumishra
    @Gofightnguyen:
    I have version 3.4.1 but the last command does not work.
    File ".\strings.py", line 5
    output=f'hello, {first_name} {last_name} '
    ^
    SyntaxError: invalid syntax
  • User profile image
    orens

    first_name = input("What is your name? ")

    last_name = input("What is your name? ")

    output = (f'Hello, {first_name} {last_name}')

    print(output.capitalize)


  • User profile image
    huuhaa
    So you cant use :

    output = f"Hello, {first_name.capitalize} {last_name.capitalize}"


    Is that correct? It doesn't give error, but prints following:

    Hello, built-in method capitalize of str object at 0x7f08c1f9ff30 built-in method capitalize of str object at 0x7f08c1fa8030

    had to edit that output cause comment section didn't show it as it was printed.
  • User profile image
    huuhaa
    @huuhaa:

    My bad, the right format obviously is:

    output = f"Hello, {first_name.capitalize()} {last_name.capitalize()}"

Add Your 2 Cents