Python Forum
string with integer variable within
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string with integer variable within
#1
Hello,

Im new to python, so forgive me if I am clumsy in my wording and if I am not providing enough information.

I am playing with writing to an 2 line LCD with a raspberry pi. I am using this example

https://github.com/adafruit/Adafruit_Pyt...har_lcd.py

# Print a two line message
lcd.message('Hello\nworld!'
Where the example says Hello on the first line, and World on the second I would like to print two different temperature readings. One on each line. I can't seem to figure out how to print some text and within that call my temp reading variable. I haven't been able to even get one line to work let alone use both lines.

lcd.message('Indoor: (tempindoor)'
lcd.message('Indoor: %d'(tempindoor)
Neither of those work
lcd.message('str(tempindoor)'
This prints my variable the way I want but I cannot figure out how to add in the text too.

Any comments are welcome.
Reply
#2
You are missing a close parenthesis on lcd.message line:,
should be:
lcd.message('Hello\nworld!')
Reply
#3
(Mar-11-2018, 10:28 PM)Larz60+ Wrote: You are missing a close parenthesis on lcd.message line:,
should be:
lcd.message('Hello\nworld!')

That was a copy/paste error. They are there in the actual program, but thank you!
Reply
#4
Have you seen anything on the LCD at all?
I took a look at the adafruit program.
If you have tried to run that code without modification, and it does not work
you need to start suspecting the hardware,
Perhaps connectors are not properly seated, reversed or something of the sort,
perhaps the device itself is damaged, or power not being properly supplied.

Look for another software routine designed for the device you are using and try that.
Search the web for other user comments on the device
You know what I'm saying here.
Reply
#5
(Mar-12-2018, 01:18 AM)Larz60+ Wrote: Have you seen anything on the LCD at all?
I took a look at the adafruit program.
If you have tried to run that code without modification, and it does not work
you need to start suspecting the hardware,
Perhaps connectors are not properly seated, reversed or something of the sort,
perhaps the device itself is damaged, or power not being properly supplied.

Look for another software routine designed for the device you are using and try that.
Search the web for other user comments on the device
You know what I'm saying here.

Yep, the demo script runs as it should on the LCD. Its only when I start trying to print a string and a variable into it that I have issues.
Reply
#6
please show your code, use BBCODE tags
Reply
#7
(Mar-12-2018, 01:41 AM)Larz60+ Wrote: please show your code, use BBCODE tags

#!/usr/bin/python
# Example using a character LCD connected to a Raspberry Pi or BeagleBone Black.
import time

import Adafruit_CharLCD as LCD


# Raspberry Pi pin configuration:
lcd_rs        = 25  # Note this might need to be changed to 21 for older revision Pi's.
lcd_en        = 24
lcd_d4        = 8
lcd_d5        = 7
lcd_d6        = 12
lcd_d7        = 16
lcd_backlight = 2

outdoor="/sys/bus/w1/devices/28-0517b184cdff/w1_slave"
indoor="/sys/bus/w1/devices/28-0517b196efff/w1_slave"


# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows    = 2

# Alternatively specify a 20x4 LCD.
# lcd_columns = 20
# lcd_rows    = 4

# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
                           lcd_columns, lcd_rows, lcd_backlight)
while True:

    with open(outdoor) as odt:
        odt_contents = odt.read()
        odt_temp = odt_contents[69:75]
        temp_outdoor = int(float(odt_temp))
        outdoortemp = temp_outdoor / 1000 * 9 / 5 + 32
        
    with open(indoor) as idt:
        idt_contents = idt.read()
        idt_temp = idt_contents[69:75]
        temp_indoor = int(float(idt_temp))
        indoortemp = temp_indoor / 1000 * 9 / 5 + 32
        

    
    # Print a two line message
    lcd.clear()
    lcd.message("")

    # Wait 5 seconds
    time.sleep(5.0)
Where lcd.message("") is I would like it to print:

Indoor: (indoortemp)
and one line 2
Outdoor: (outdoortemp)

Basically print text and then the variable.
Reply
#8
try the following:
    lcd.clear()
    msg = 'Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp)
    lcd.message(msg)
Note that there are no quotes around msg. That's because it is a variable.

you should be able to do it this way as well
    lcd.clear()
    lcd.message('Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp))
Reply
#9
(Mar-12-2018, 02:46 AM)Larz60+ Wrote: try the following:
    lcd.clear()
    msg = 'Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp)
    lcd.message(msg)
Note that there are no quotes around msg. That's because it is a variable.

you should be able to do it this way as well
    lcd.clear()
    lcd.message('Indoor: {}\nOutdoor: {}'.format(indoortemp, outdoortemp))

That works! Very grateful for your help! Thank you! I'll see if I can do some reading on what exactly is happening there. Ive never used .format before.
Reply
#10
if you are running python 3.6, there is an even better improvement over format, it's called f-string,
and for your code, the above would be coded like:
msg = f'Indoor: {indoortemp}\nOutdoor: {outdoortemp}'
which is slick!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 787 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Need help on how to include single quotes on data of variable string hani_hms 5 2,030 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,818 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 964 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,288 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,783 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,300 Jul-11-2022, 08:52 AM
Last Post: japo85
  Can you print a string variable to printer hammer 2 1,949 Apr-30-2022, 11:48 PM
Last Post: hammer
Question How to convert string to variable? chatguy 5 2,399 Apr-12-2022, 08:31 PM
Last Post: buran
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,528 May-24-2021, 08:48 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020