r/learnpython • • Sep 01 '24

cant understand why I cant use '('

I I need it to print a phone number and cant understand why I cant use '('

phone_number = int(input())

first_three = phone_number //10000000
pre_middle_three = phone_number // 100
post_middle_three = pre_middle_three % 1000
last_four = phone_number % 10000
print('('first_three')',post_middle_three,-last_four)

I am very new to python. Thank you

0 Upvotes

19 comments sorted by

8

u/rodrigowb4ey Sep 01 '24

don't use integers and floats when you don't actually need to perform calculations on you data. since your data is just a phone number whose digits you want to separate in chunks (first three, last four and etc), you can save it as a string and separate the chunks based on slicings of the original string.

ex: phone_number[2:6] returns a slice of the original string, starting from index 2 up until index 6, which is not included.

phone_number = "12345-6789" print(phone_number[:5]) // "12345" print(phone_number[6:]) // "6789"

to print them in a formatted way, you can use f-strings

template = f"(278) {phone_number[:5]}-{phone_number[6:]}" print(template) // "(278) 12345-6789"

13

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 01 '24

It's not the parentheses that are a problem, Python is just complaining because you can't have string literals adjacent to names without operators in-between.

print('('first_three')',post_middle_three,-last_four)

If you put commas there, that'll fix the error, but now you have spaces there in the output.

print('(', first_three, ')', post_middle_three, -last_four)

If you use string concatenation instead, there are no spaces but this takes more effort.

print('(' + str(first_three) + ')', post_middle_three, -last_four)

An f-string would be preferable.

print(f"({first_three})", post_middle_three, -last_four)

However I'm not sure what kind of output you're expecting.

8

u/nog642 Sep 01 '24

If you're going to use an f-string, might as well go all the way.

print(f'({first_three}) {post_middle_three}-{last_four}')

6

u/pgpndw Sep 01 '24 edited Sep 01 '24

There will also be issues with leading zeroes in any of the parts the phone number gets split into. It's better not to convert the input to an integer in the first place. Just keep it and its parts as strings throughout.

6

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 01 '24 edited Sep 01 '24

True. Phone numbers aren't really numbers at all from our point of view, so they should 100% be treated as strings.

EDIT: Or, better yet, parsed into distinct PhoneNumber objects to ensure you wouldn't need to perform validation all over the place. You can read more by searching about "primitive obsession".

2

u/mrswats Sep 01 '24

You can use f-strings: https://docs.python.org/3/reference/lexical_analysis.html#f-strings, or add commas between the different arguments you are passing to print.

2

u/[deleted] Sep 01 '24

you are trying to format the string?

print(f”({first_three})-{post_middle_three}-{last_four}”)

3

u/AdmirableOstrich Sep 01 '24

The parentheses aren't the problem. When using the multi-argument print statement, everything needs to be separated by commas. '(' is a valid string, but '('firststring')' is not.

The actual solution here is to use an f-string, but if you want to use your code as-is, every argument to the print function has to be separated.

1

u/randomonetwo34567890 Sep 01 '24

You need to use double quotes if you want to print quotes:

print("('first_three')") - will print ('first_three') which probably isn't what you're looking for.

You need to use f string to print what you want, like this:

print(f"('{first_three}')",post_middle_three,-last_four)

3

u/[deleted] Sep 01 '24

[removed] — view removed comment

0

u/randomonetwo34567890 Sep 01 '24

Then just remove one of the quotes, simple. I have no clue how phone numbers look like in the US

1

u/OkVariables Sep 01 '24 edited Sep 01 '24

When executing the code, I got the error
"SyntaxError: invalid syntax. Perhaps you forgot a comma?"
So why don't we try that?

phone_number = int(input())

first_three = phone_number //10000000
pre_middle_three = phone_number // 100
post_middle_three = pre_middle_three % 1000
last_four = phone_number % 10000
print('(',first_three,')',post_middle_three,-last_four)

>( 123 ) 678 -7890

Now you have spaces between the brackets and the first three digits.

So you might want to look at Formatted Strings: 7. Input and Output — Python 3.12.5 documentation
Then you could do something like this

phone_number = int(input())

first_three = phone_number //10000000
pre_middle_three = phone_number // 100
post_middle_three = pre_middle_three % 1000
last_four = phone_number % 10000
print(f'({first_three}) {post_middle_three}-{last_four}')

1

u/Kriss3d Sep 01 '24

Why not just go with printing beteeen indexes? print(phone_number[0-2]"_"phone_number[3-5]" _"phone_number[6-9]

This should work simply by printing the variable but only the parts of it that is beteeen the indexes and add the dash beteeen.

1

u/w8eight Sep 01 '24
print(f"({first_three})")

1

u/[deleted] Sep 01 '24

i just take the number as a string then do first_three[:3] and so on. The error in the code is in

print('('first_three')',post_middle_three,-last_four)

the correct code is

print('(',first_three,')',post_middle_three,-last_four)

,s are important

0

u/throwaway6560192 Sep 01 '24

'('first_three')'

You can't put two things next to each other with no separator (comma) or operator in between. (Except if they're two constant strings, in which case they get joined, but that's not the case here.)

You want one of:

print('(', first_three, ')', ...)  # will add a space between the parentheses and the first_three

print('(' + str(first_three) + ')', ...)

print(f"({first_three} {post_middle_three} {-last_four}")  # this is the best way in modern Python