President Carter laid down a hard line toward Cuba today, saying that the United States will not recognize the Castro regime until Cuba releases its "thousands of political prisoners" and ends its "unwarranted involvement" in the affairs of other countries.

Employing some of the harshest language he has used on the subject, the president told a questioner at a town meeting here that Cuba is little more than a surrogate of the Soviet Union bent on fomenting trouble throughout the Western Hemisphere.

He also accused Castro of maintaining "the most heavily militarized country on earth" and of "constantly trying to stir up the people of Puerto Rico to seek independence from the United States, which the Puerto Rican people do not want.

"There's no way that Cuba could survive economically with their communistic experiment 90 miles from our country without the Soviets propping them up," Carter said. "Every day the Soviet Union sends to Castro $8 million just to keep the Cuban people alive economically and to keep Castro in power. . .

"So I believe," he continued, "that until Cuba can bring their own troops back from unwarranted involvement in the internal affairs of other countries, until they release the hundreds and hundreds, even thousands of the political prisoners they have in jail -- and some of them have been there over 20 years -- and until they stop interfering in the internal affairs of countries even in this hemisphere, we will not recognize Cuba. After that, we'll consider it."

The president's statement came at a town meeting which, like similar sessions Carter had held recently, was dominated by concern over domestic issues, particularly inflation and energy.

With an audience of more than 1,000 jammed into the Thornridge High School gymnasium looking on, the president sprinkled his remarks with glowing references to the recent visit of Pope John Paul II and extolled the virtues he said the pope stands for -- "Truth, integrity, humility, gentleness, the strength of families. . . ."

He also emphasized his desire "to keep the government's nose" out of local affairs, a subject of some sensitivity in the Chicago area, where the city's school district is fighting a federal desegregation order.

But when asked directly about the desegregation dispute, the president quickly noted that he has no authority over its outcome, and suggested that Chicago might be better off with a decision handed down by the local federal court, which could lead to court-ordered busing, than to continue the protracted dispute with the Department of Health, Education and Welfare.