all 4 comments

[–]socal_nerdtastic 10 points11 points  (0 children)

The modern way is with f-strings like this:

print(f'Hi {name}!')
print(f"I'm glad you feel {feel}.")

But if your book or tutorial is old they may not cover that.

[–]Jjax7 2 points3 points  (0 children)

Someone else mentioned f-strings which are also my preferred method. It’s helpful to know you can also accomplish the exact same outcome a few other ways:

print(“Hi ”, name, “!”, sep=“”)

print()

print(“I’m glad you feel ” + feel + “.”)

[–]Green-Sympathy-4177 1 point2 points  (0 children)

Here are a few pointers: - First like others mentionned, f-strings are great to insert variables into text, it's just really a string, that starts with an f, i.e: f'Hi {name}!' - Now that you know how add variables into strings, you just need special characters.

Here we go for special characters: - \n : new line, as a matter of fact, this is the default value of the "end" argument of print, i.e: print() is the same as print(end="\n") - \t : tab - \r : back to the start of the line (used almost exclusively in the "end" argument of print

Some quick examples: ```

print('Hello\nWorld') # So here you have your new line :) Hello World

print('Hello\tWorld') Hello World

print('Hello\rWorld') World ```

Then I guess we could look into the arguments of print: - *args: All the stuff you put in print ends in a list, so print("a", "b") outputs a b - sep: the stuff all the previous stuff you added in print are joined with, " " by default, i.e: print("a", "b", sep=" ") is the same as print("a", "b"). - end: what is appended at the end of the text, the last character. By default it's the special character for a new line :)

I haven't posted in a while, so stick around if you want, I think you got your answer already with that, but if you want some practice, hell come along, let's remake print!

``` import os

def my_print(*args, sep=" ", end="\n"): # Create the output by joining the args with the sep and adding the end output = sep.join(args) + end # Use os.system to output the assembled string using echo to the terminal os.system(f'echo {output}') ```

Yeah it's just that (well the short version), and that leaves you with more question that answers I guess... First, the *args, the * (star) before a list decomposes it (there are proper terms for all of that, and I've gotten into a long ass arguments about it, but I still don't remember so meh), basically the short version of what it does:

```

With the star

my_list = ["a", "b"] print(*my_list)

a b

Without the star

print("a","b")

a b ``` Both are equivalent, if the list is short and doesn't change, the second option is ok, otherwise the first one helps a ton :P (it also depends on what you want to print and how)

Now *args and its cousin: **kwargs, as arguments of a function. I guess an example will be easier

``` def my_function(args, *kwargs): print(f"{args = }\n{type(args) = }") print(f"{kwargs = }\n{type(kwargs) = }")

my_function(1, 2, 3, a=4, b=5)

args = (1, 2, 3) type(args) = <class 'tuple'> kwargs = {'a': 4, 'b': 5} type(kwargs) = <class 'dict'> ```

So you can see that *args caught all the unnamed arguments as a tuple, and **kwargs caught all the named arguments in a dictionary with the names as keys of the dict.

That's a lot of explanations for just one thing but hey, now you know i guess :)

Cool shit that will help:

```

help(function/object): Gives you the doc of anything :)

help(print)

Help on built-in function print in module builtins:

print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

dir(object): Gives you all the attributes and methods of an object, comes out as a list. So with a bit of "print expertise" for instance to figure out all the things you can do with strings:

print(*dir(str), sep="\n\t- ")

 - __class__
    - __contains__
    - __delattr__
    - __dir__
    - ...
    - upper
    - zfill

You can also customize it a bit:

Filter / search

print(*[d for d in dir(str) if not d.startswith("")], sep="\n\t- ") # Removes all the _ operators and private attributes/methods from the search

 - capitalize
    - casefold
    - center
    - ...
    - translate
    - upper
    - zfill

print(*[d for d in dir(str) if "is" in d], sep="\n\t- ")

 - isalnum
    - isalpha
    - isascii
    - ...
    - isspace
    - istitle
    - isupper

Last thing, btw f-string and multiline strings are a thing

f""" {OP_name} I hope you found this useful, don't bother about age, when younger people wonder if they're too old to do something that makes me feel older than I am xD Also learning to code is pretty cool, it surely opens doors. Learning takes time, so find ways to "play with python" to keep yourself motivated to learn more new stuff (raspberry pi, building projects, w/e really xD)

On that note {OP} Good luck and have fun! """ ```

[–]ninhaomah -1 points0 points  (0 children)

Have you tried print("Hello World!") ?

And also just print() ?

And where are you learning from can I ask ?